On 02/23/2016 10:47 AM, Piotr Dobrogost wrote:
Hi!

I'm getting AssertionError: Dependency rule tried to blank-out primary
key column 'text_value.text_id' on instance '<TextValue at
0x7f6b260d9710>' error while trying to update row using association
proxy ('values') like this:

|
session.add(Text(values={'pl':u'org','en':u'org'}))
text =session.query(Text).one()
text.values ={'pl':u'modified','en':u'modified'}
session.commit()

|

Working example is at
https://gist.github.com/piotr-dobrogost/74073cf11006fb68e555
What am I doing wrong?


well the first thing is you definitely don't need UniqueConstraint('lang_id', 'text_id') because these columns are already in the primary key.

the next issue in this test at least is that you're referring to the primary key of a row in Lang but there is no Lang row being created here. SQLite will let you put the invalid primary key into lang_id but only because foreign keys aren't enforced by default.

then the reason for the error is that TextValue objects are potentially being replaced but the mapping is not emitting a delete for the old TextValue, so you'd need to put cascade="all, delete-orphan" on the Text._values relationship so that when you assign a new dictionary value, the old TextValue objects are deleted.

In this specific example, marking the TextValue as deleted ends up gving you an UPDATE of TextValue, because the ORM converts an INSERT/DELETE of the same primary key into a single UPDATE. But if your dictionary removed some of those keys you'd need a DELETE so the cascade rule should be applied here.






Best regards,
Piotr Dobrogost

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to