Hi,
I'm not sure if the problem I've got is a bug or intended behaviour.
Here's a test script:
#########################################
import sqlalchemy as sa
import sqlalchemy.orm as saorm
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
ACTIVE_HISTORY = True
class Obj(Base):
__tablename__ = 'obj'
id = sa.Column(sa.Integer, primary_key=True)
with_default = saorm.column_property(
sa.Column(sa.String(), default='default'),
active_history=ACTIVE_HISTORY,
)
with_server_default = saorm.column_property(
sa.Column(sa.String(), server_default='server_default'),
active_history=ACTIVE_HISTORY,
)
with_no_default = saorm.column_property(
sa.Column(sa.String()),
active_history=ACTIVE_HISTORY,
)
if __name__ == '__main__':
engine = sa.create_engine('sqlite://', echo='debug')
Base.metadata.create_all(engine)
session = saorm.sessionmaker(bind=engine)()
o = Obj()
session.add(o)
session.flush()
attrs = ['with_default', 'with_server_default', 'with_no_default']
for attr in attrs:
setattr(o, attr, 'new value')
for attr in attrs:
print '%20s: %s' % (attr, saorm.attributes.get_history(o, attr))
#########################################
And here's the output:
with_default: History(added=['new value'], unchanged=(),
deleted=['default'])
with_server_default: History(added=['new value'], unchanged=(),
deleted=[u'server_default'])
with_no_default: History(added=['new value'], unchanged=(), deleted=())
The last line is the one I have a problem with. I'm trying to write a
library module that uses the before_flush event to log (before, after)
tuples for every change to certain tables. I had an assertion that the
"added" and "deleted" values were both lists of length 1 (because I'm
only looking at scalar columns), but the assertion was failing in some
cases. I think I tracked it down to the above case of an object that
was flushed without an explicit default having been assigned.
In the SQL output I can see that the with_no_default column was
explicitly inserted as NULL, and it has active_history=True, so I
can't see a reason why the "deleted" part of the history is empty. Is
this a bug, or an invalid assumption by me? As a workaround, is it
safe to assume that any empty "deleted" values mean that the column
was NULL (given active history, and that the value has actually
changed)?
Thanks a lot,
Simon
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.