【发布时间】:2015-10-24 11:02:13
【问题描述】:
我正在尝试从 sql-alchemy 中的多对多关系中删除一个子对象。
我不断收到以下错误:
StaleDataError: DELETE statement on table 'headings_locations' expected to delete 1 row(s); Only 2 were matched.
我查看了一些现有的 stackexchange 问题 (SQLAlchemy DELETE Error caused by having a both lazy-load AND a dynamic version of the same relationship, SQLAlchemy StaleDataError on deleting items inserted via ORM sqlalchemy.orm.exc.StaleDataError, SQLAlchemy Attempting to Twice Delete Many to Many Secondary Relationship, Delete from Many to Many Relationship in MySQL) 关于这一点以及阅读文档,但无法弄清楚它为什么不起作用。
我定义关系的代码如下:
headings_locations = db.Table('headings_locations',
db.Column('id', db.Integer, primary_key=True),
db.Column('location_id', db.Integer(), db.ForeignKey('location.id')),
db.Column('headings_id', db.Integer(), db.ForeignKey('headings.id')))
class Headings(db.Model):
__tablename__ = "headings"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
version = db.Column(db.Integer, default=1)
special = db.Column(db.Boolean(), default=False)
content = db.relationship('Content', backref=db.backref('heading'), cascade="all, delete-orphan")
created_date = db.Column(db.Date, default=datetime.datetime.utcnow())
modified_date = db.Column(db.Date, default=datetime.datetime.utcnow(), onupdate=datetime.datetime.utcnow())
def __init__(self, name):
self.name = name
class Location(db.Model):
__tablename__ = "location"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
account_id = db.Column(db.Integer, db.ForeignKey('account.id'))
version = db.Column(db.Integer, default=1)
created_date = db.Column(db.Date, default=datetime.datetime.utcnow())
modified_date = db.Column(db.Date, default=datetime.datetime.utcnow())
location_prefix = db.Column(db.Integer)
numbers = db.relationship('Numbers', backref=db.backref('location'), cascade="all, delete-orphan")
headings = db.relationship('Headings', secondary=headings_locations,
backref=db.backref('locations', lazy='dynamic', cascade="all"))
def __init__(self, name):
self.name = name
而我的删除代码如下:
@content_blueprint.route('/delete_content/<int:location_id>/<int:heading_id>')
@login_required
def delete_content(location_id, heading_id):
import pdb
pdb.set_trace()
location = db.session.query(Location).filter_by(id = location_id).first()
heading = db.session.query(Headings).filter_by(id = heading_id).first()
location.headings.remove(heading)
#db.session.delete(heading)
db.session.commit()
flash('Data Updated, thank-you')
return redirect(url_for('content.add_heading', location_id=location_id))
无论我尝试以哪种方式删除子对象(db.session.delete(heading) 或 location.headings.remove(heading),我仍然会遇到同样的错误。
非常感谢任何帮助。
我的数据库是 postgresql。
编辑: 我添加关系的代码:
new_heading = Headings(form.new_heading.data)
db.session.add(new_heading)
location.headings.append(new_heading)
db.session.commit()
【问题讨论】:
-
一个谦虚的建议:在任何一种 ORM 中调试类似情况的最快方法是查看系统生成的表定义,然后查看生成的 SQL 查询。通常会立即清楚错误的原因。如果不是这种情况,则直接向 DBMS 询问该查询(例如使用 pgAdmin),并查看生成的错误消息。
-
@rob123 我觉得你们的关系不是
many to many和Locations and Headings之间的关系,好像你们之间有one to many,比如one Heading can have many Locations,理想情况下你不能通过删除HeadingsLocations因为你有一个ForeignKey关系,这是因为你试图删除的heading可能有另一个Location,这会阻止你的删除。
标签: postgresql python-2.7 flask sqlalchemy flask-sqlalchemy