Re: [Sqlalchemy-tickets] [sqlalchemy] #2793: support server default for version_id_col
Brought to you by:
zzzeek
|
From: sqlalchemy <mi...@zz...> - 2013-07-31 20:36:33
|
#2793: support server default for version_id_col
------------------------------+-------------------------------
Reporter: zzzeek | Owner: zzzeek
Type: enhancement | Status: new
Priority: medium | Milestone: 0.9.0
Component: orm | Severity: major - 1-3 hours
Resolution: | Keywords:
Progress State: in queue |
------------------------------+-------------------------------
Description changed by zzzeek:
Old description:
> {{{
> #!diff
> diff --git a/lib/sqlalchemy/orm/persistence.py
> b/lib/sqlalchemy/orm/persistence.py
> index 46b0be9..6013095 100644
> --- a/lib/sqlalchemy/orm/persistence.py
> +++ b/lib/sqlalchemy/orm/persistence.py
> @@ -248,7 +248,9 @@ def _collect_insert_commands(base_mapper,
> uowtransaction, table,
> has_all_pks = True
> for col in mapper._cols_by_table[table]:
> if col is mapper.version_id_col:
> - params[col.key] = mapper.version_id_generator(None)
> + val = mapper.version_id_generator(None)
> + if val is not None:
> + params[col.key] = val
> else:
> # pull straight from the dict for
> # pending objects
> @@ -315,19 +317,21 @@ def _collect_update_commands(base_mapper,
> uowtransaction,
> params[col.key] = history.added[0]
> hasdata = True
> else:
> - params[col.key] = mapper.version_id_generator(
> + val = mapper.version_id_generator(
> params[col._label])
> -
> - # HACK: check for history, in case the
> - # history is only
> - # in a different table than the one
> - # where the version_id_col is.
> - for prop in mapper._columntoproperty.itervalues():
> - history = attributes.get_state_history(
> - state, prop.key,
> - attributes.PASSIVE_NO_INITIALIZE)
> - if history.added:
> - hasdata = True
> + if val is not None:
> + params[col.key] = val
> +
> + # HACK: check for history, in case the
> + # history is only
> + # in a different table than the one
> + # where the version_id_col is.
> + for prop in
> mapper._columntoproperty.itervalues():
> + history = attributes.get_state_history(
> + state, prop.key,
> + attributes.PASSIVE_NO_INITIALIZE)
> + if history.added:
> + hasdata = True
> else:
> prop = mapper._columntoproperty[col]
> history = attributes.get_state_history(
> }}}
>
> {{{
> #!python
> from sqlalchemy import *
> from sqlalchemy.orm import *
> from sqlalchemy.ext.declarative import declarative_base
>
> Base = declarative_base()
>
> class A(Base):
> __tablename__ = 'a'
>
> id = Column(Integer, primary_key=True)
> data = Column(String)
> version_id = Column('xmin', Integer, server_default=FetchedValue())
>
> __mapper_args__ = {
> "version_id_col": version_id,
> "version_id_generator": lambda val: None
> }
>
> engine = create_engine("postgresql://scott:tiger@localhost/test",
> echo=True)
> Base.metadata.drop_all(engine)
> engine.execute("create table a (id serial primary key, data varchar)")
>
> s = Session(engine)
>
> a1 = A(data='a1')
>
> s.add(a1)
>
> s.flush()
>
> a1.data = 'a2'
>
> s.flush()
> }}}
New description:
{{{
#!python
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
data = Column(String)
version_id = Column('xmin', Integer,
server_default=FetchedValue(),
server_onupdate=FetchedValue())
__mapper_args__ = {
"version_id_col": version_id,
"version_id_generator": lambda val: None,
# "eager_defaults": True # should not be needed
}
# should work whether or not this is set
# __table_args__ = {"implicit_returning": False}
engine = create_engine("postgresql://scott:tiger@localhost/test",
echo=True)
Base.metadata.drop_all(engine)
engine.execute("create table a (id serial primary key, data varchar)")
s = Session(engine)
a1 = A(data='a1')
s.add(a1)
s.flush()
a1.data = 'a2'
s.flush()
}}}
--
--
Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2793#comment:2>
sqlalchemy <http://www.sqlalchemy.org/>
The Database Toolkit for Python
|