-
-
Save piotr-dobrogost/74073cf11006fb68e555 to your computer and use it in GitHub Desktop.
SQLAlchemy AssertionError: Dependency rule tried to blank-out primary key column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
from sqlalchemy import Column, ForeignKey, Integer, String, Unicode, UniqueConstraint | |
from sqlalchemy.ext.associationproxy import association_proxy | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import relationship, sessionmaker | |
from sqlalchemy.orm.collections import attribute_mapped_collection | |
Base = declarative_base() | |
class Text(Base): | |
__tablename__ = 'text' | |
id = Column(Integer, primary_key=True) | |
_values = relationship('TextValue', collection_class=attribute_mapped_collection('lang_id')) | |
values = association_proxy('_values', 'value', creator=lambda k, v: TextValue(lang_id=k, value=v)) | |
class Lang(Base): | |
__tablename__ = 'lang' | |
id = Column(String(4), primary_key=True) | |
name = Column(String(20)) | |
class TextValue(Base): | |
__tablename__ = 'text_value' | |
__table_args__ = (UniqueConstraint('lang_id', 'text_id'),) | |
text_id = Column(Integer, ForeignKey(Text.id), primary_key=True) | |
lang_id = Column(Integer, ForeignKey(Lang.id), primary_key=True) | |
value = Column(Unicode) | |
engine = create_engine('sqlite:////tmp/test.db') | |
Base.metadata.create_all(engine) | |
Session = sessionmaker() | |
Session.configure(bind=engine) | |
session = Session() | |
values = {'pl': u'org', 'en': u'org'} | |
session.add(Text(values=values)) | |
text = session.query(Text).one() | |
text.values = {'pl': u'modified', 'en': u'modified'} | |
session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "/home/piotr/.PyCharm50/config/scratches/scratch_20", line 41, in <module> | |
session.commit() | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 813, in commit | |
self.transaction.commit() | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 392, in commit | |
self._prepare_impl() | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl | |
self.session.flush() | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2027, in flush | |
self._flush(objects) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2145, in _flush | |
transaction.rollback(_capture_exception=True) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__ | |
compat.reraise(exc_type, exc_value, exc_tb) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2109, in _flush | |
flush_context.execute() | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute | |
rec.execute(self) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 487, in execute | |
self.dependency_processor.process_saves(uow, states) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/dependency.py", line 558, in process_saves | |
uowcommit, False) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/dependency.py", line 575, in _synchronize | |
sync.clear(dest, self.mapper, self.prop.synchronize_pairs) | |
File "/home/piotr/.virtualenvs/pab/lib/python2.7/site-packages/sqlalchemy/orm/sync.py", line 74, in clear | |
(r, orm_util.state_str(dest)) | |
AssertionError: Dependency rule tried to blank-out primary key column 'text_value.text_id' on instance '<TextValue at 0x7f6b260d9710>' | |
Process finished with exit code 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment