[Sqlalchemy-tickets] Issue #3465: using version_id_col & version_id_generator: False makes SQLAlche
Brought to you by:
zzzeek
|
From: tonymillion <iss...@bi...> - 2015-06-24 23:44:25
|
New issue 3465: using version_id_col & version_id_generator: False makes SQLAlchemy emit bad SQL on relationship update https://bitbucket.org/zzzeek/sqlalchemy/issue/3465/using-version_id_col-version_id_generator tonymillion: I have a model as such (in this case I'm using Flask-SQLAlchemy, but thats not relevant to the error): ``` class Person(BaseModel): <snip - stuff for model, id as primary key etc> purchase_uuid = db.Column(postgresql.UUID(as_uuid=True), default=uuid.uuid4) __mapper_args__ = { 'version_id_col': purchase_uuid, 'version_id_generator': False } ``` this works fine i.e. we can block a certain `UPDATES` unless purchase_uuid is being updated (taken from http://docs.sqlalchemy.org/en/latest/orm/versioning.html#programmatic-or-conditional-version-counters ) We also have another model ``` class Session(BaseModel): <snip - stuff for model, id as primary key etc> # the person who is represented by this Session person_id = db.Column(db.Integer, db.ForeignKey("people.id"), index=True) person = db.relationship("Person", # backref=db.backref("sessions", lazy="dynamic"), lazy="joined", uselist=False, foreign_keys=[person_id]) ``` When I uncomment the backref and then create a session object and assign a person: ``` session = Session() session.person = person db.session.add(session) db.session.commit() ``` I get the following: ``` ProgrammingError: (psycopg2.ProgrammingError) syntax error at or near "WHERE" LINE 1: UPDATE people SET WHERE people.id = 1 AND people.purchase_u... ^ [SQL: 'UPDATE people SET WHERE people.id = %(people_id)s AND people.purchase_uuid = %(people_purchase_uuid)s RETURNING people.purchase_uuid'] [parameters: {'people_id': 1, 'people_purchase_uuid': UUID('e3a73745-0979-42e3-90c5-72e6a32219c7')}] ``` As you can see SQLAlchemy is outputting some invalid SQL i.e. `UPDATE people SET WHERE` If I comment out either the backref (the `sessions` relationship) or the `__mapper_args__` the problem goes away. |