[Sqlalchemy-tickets] Issue #4194: can't specify select-bound versioning column (zzzeek/sqlalchemy)
Brought to you by:
zzzeek
From: Michael B. <iss...@bi...> - 2018-02-28 00:44:27
|
New issue 4194: can't specify select-bound versioning column https://bitbucket.org/zzzeek/sqlalchemy/issues/4194/cant-specify-select-bound-versioning Michael Bayer: this is dependent on #4193 being resolved ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() version_table = Table( 'version_table', Base.metadata, Column('id', Integer, primary_key=True), Column('version_id', Integer, nullable=False), Column('value', String(40), nullable=False)) current = version_table.select().\ where(version_table.c.id > 0).alias('current_table') class Versioned(Base): __table__ = current __mapper_args__ = { # won't insert, # "version_id_col": current.c.version_id # inserts assuming we fixed #4193, but then expires the column "version_id_col": version_table.c.version_id } e = create_engine("sqlite://", echo=True) Base.metadata.create_all(e) s = Session(e) v1 = Versioned(value='x') s.add(v1) s.flush() print("-------------------") # emits SQL if we mapped to table-bound column print(v1.version_id) ``` this is because: ``` #!diff diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index dc0ae1c38..07fb50d6e 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -429,6 +429,10 @@ def _collect_insert_commands( else: has_all_defaults = has_all_pks = True + # HERE vvvvvvvvvvvvv + # version_id_col needs to be the table bound column. + # the only way it could "work" the other way would be if we maintain + # corresponding column and all that. if mapper.version_id_generator is not False \ and mapper.version_id_col is not None and \ mapper.version_id_col in mapper._cols_by_table[table]: @@ -1081,7 +1085,6 @@ def _finalize_insert_update_commands(base_mapper, uowtransaction, states): """ for state, state_dict, mapper, connection, has_identity in states: - if mapper._readonly_props: readonly = state.unmodified_intersection( [ ``` |