当我从我的数据库中删除一个项目时,我得到了一个奇怪的级联删除(我没有配置)。删除项对应的分类条目被删除。我如何阻止这种情况发生?
这是我的数据库设置:
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
class User(Base):
"""Object holding information about individual registered users"""
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
email = Column(String(250), nullable=False)
picture = Column(String(250))
class Category(Base):
"""Object holding information about item categories"""
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
class Item(Base):
"""Object holding information about individual catalog items"""
__tablename__ = 'item'
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)
description = Column(String(5000), nullable=False)
category_id = Column(Integer, ForeignKey('category.id'))
category = relationship("Category")
user_id = Column(Integer, ForeignKey('user.id'))
user = relationship("User")
engine = create_engine('postgresql+psycopg2://********:********'
+ '/itemcatalog.db', echo=False)
Base.metadata.create_all(engine)
这是触发删除的代码:
@app.route('/item/<int:item_id>/delete/', methods=['GET', 'POST'])
def delete_item(item_id):
item = session.query(Item).filter_by(id=item_id).one()
if request.method == 'POST':
session.delete(item)
session.commit()
flash('Item Successfully Deleted')
return redirect(url_for('show_catalog'))
else:
return render_template('deleteitem.html', item=item,
username=login_session['username'],
picture=login_session['picture'])
预期的行为是仅删除由其“id”指定的项目,并且不会更改其他表。
我得到的实际结果是该item被删除,该item对应的category表中的条目也被删除了。
更新:
我正在采取的步骤是:
显示这一点的日志(和删除)如下。
添加新项目:
2017-12-06 19:32:16 UTC [5383-22] [email protected] LOG: statement: SELECT category.id AS category_id, category.name AS category_name
FROM category
2017-12-06 19:32:16 UTC [5383-23] [email protected] LOG: statement: INSERT INTO item (name, description, category_id, user_id) VALUES ('Coffee', 'tasty beverage', 3, 2) RETURNING item.id
2017-12-06 19:32:16 UTC [5383-24] [email protected] LOG: statement: COMMIT
2017-12-06 19:32:16 UTC [5383-25] [email protected] LOG: statement: BEGIN
2017-12-06 19:32:16 UTC [5383-26] [email protected] LOG: statement: SELECT category.id AS category_id, category.name AS category_name
FROM category ORDER BY category.name ASC
2017-12-06 19:32:16 UTC [5383-27] [email protected] LOG: statement: SELECT item.id AS item_id, item.name AS item_name, item.description AS item_description, item.category_id AS item_category_id, item.user_id AS item_user_id
FROM item ORDER BY item.name ASC
删除新项目:
2017-12-06 19:33:14 UTC [5383-30] [email protected] LOG: statement: SELECT item.id AS item_id, item.name AS item_name, item.description AS item_description, item.category_id AS item_category_id, item.user_id AS item_user_id
FROM item
WHERE item.id = 7
2017-12-06 19:33:14 UTC [5383-31] [email protected] LOG: statement: SELECT category.id AS category_id, category.name AS category_name
FROM category
WHERE category.id = 3
2017-12-06 19:33:14 UTC [5383-32] [email protected] LOG: statement: SELECT item.id AS item_id, item.name AS item_name, item.description AS item_description, item.category_id AS item_category_id, item.user_id AS item_user_id
FROM item
WHERE 3 = item.category_id
2017-12-06 19:33:14 UTC [5383-33] [email protected] LOG: statement: UPDATE item SET category_id=NULL WHERE item.id = 5
2017-12-06 19:33:14 UTC [5383-34] [email protected] LOG: statement: UPDATE item SET category_id=NULL WHERE item.id = 6
2017-12-06 19:33:14 UTC [5383-35] [email protected] LOG: statement: DELETE FROM item WHERE item.id = 7
2017-12-06 19:33:14 UTC [5383-36] [email protected] LOG: statement: DELETE FROM category WHERE category.id = 3
2017-12-06 19:33:14 UTC [5383-37] [email protected] LOG: statement: COMMIT
2017-12-06 19:33:14 UTC [5383-38] [email protected] LOG: statement: BEGIN
2017-12-06 19:33:14 UTC [5383-39] [email protected] LOG: statement: SELECT category.id AS category_id, category.name AS category_name
FROM category ORDER BY category.name ASC
2017-12-06 19:33:14 UTC [5383-40] [email protected] LOG: statement: SELECT item.id AS item_id, item.name AS item_name, item.description AS item_description, item.category_id AS item_category_id, item.user_id AS item_user_id
FROM item ORDER BY item.name ASC
从日志中我可以看到它的行为不像我想要的那样,但我不知道要改变什么来阻止这种行为。
感谢您的回复 larsks,并为您的困惑感到抱歉。我的错误很愚蠢……我创建了一个新的数据库设置文件用于测试目的,但忘记在我的主 app.py 文件中更新它的路径。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句