Discussion:
colum with server_default does not have new value after session.merge
Victor Varvariuc
2013-10-17 10:34:05 UTC
Permalink
I have a model:

class ExchangeRate(Base):
...
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(DateTime, nullable=False, server_default=func.now(),
server_onupdate=func.now())

Model fields `created_at` and `updated_at` have values generated by server,
but the value is not fetched automatically:

14 with Session() as session:
---> 15 return session.merge(exchange_rate)
16

ipdb> exchange_rate
ExchangeRate(currency_from='RUB', currency_to='KZT', rate=Decimal('4.77'),
rounding_type='FLOOR', rounding_precision=-2)
ipdb> session.merge(exchange_rate)
ExchangeRate(currency_from='RUB', currency_to='KZT', rate=Decimal('4.77'),
rounding_type='FLOOR', rounding_precision=-2)
ipdb>

Do I do use it correctly?
--
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 sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Victor Varvariuc
2013-10-17 10:35:49 UTC
Permalink
Not sure if this matters, but the model doesn't have an autoincrement
primary key:

class ExchangeRate(Base):
currency_from = Column(CURRENCY_ISO_CODE_TYPE, primary_key=True)
currency_to = Column(CURRENCY_ISO_CODE_TYPE, primary_key=True)
...
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(DateTime, nullable=False, server_default=func.now(),
server_onupdate=func.now())
--
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 sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-10-17 14:07:27 UTC
Permalink
Post by Victor Varvariuc
...
created_at = Column(DateTime, nullable=False, server_default=func.now())
updated_at = Column(DateTime, nullable=False, server_default=func.now(),
server_onupdate=func.now())
---> 15 return session.merge(exchange_rate)
16
ipdb> exchange_rate
ExchangeRate(currency_from='RUB', currency_to='KZT', rate=Decimal('4.77'), rounding_type='FLOOR', rounding_precision=-2)
ipdb> session.merge(exchange_rate)
ExchangeRate(currency_from='RUB', currency_to='KZT', rate=Decimal('4.77'), rounding_type='FLOOR', rounding_precision=-2)
ipdb>
Do I do use it correctly?
if the question is, why don't you see created_at, updated_at, those would go in when the object is flushed, and then you'd only see them on the instance if you access them first, as server-generated defaults aren't automatically fetched back during flush (an improved "eager_defaults" feature in 0.9 makes this feasible, however: http://docs.sqlalchemy.org/en/latest/changelog/migration_09.html#orm-can-efficiently-fetch-just-generated-insert-update-defaults-using-returning).
Post by Victor Varvariuc
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Victor Varvariuc
2013-10-17 14:34:26 UTC
Permalink
Ok, so the answer is to use 'eager_defaults': True :

class CommonBase(object):
"""Base model for Apilib db-mapped and virtual models.
"""
__mapper_args__ = {
# immediately fetch the value of server-generated default values
after an INSERT or UPDATE
'eager_defaults': True,
}

Looks like it works, but in other models without columns with
'server_default' I am getting:

File "../python2.7/site-packages/sqlalchemy/orm/query.py", line 2685, in
_compile_context
"No column-based properties specified for "
InvalidRequestError: No column-based properties specified for refresh
operation. Use session.expire() to reload collections and related items.
--
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 sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-10-17 16:46:38 UTC
Permalink
Post by Victor Varvariuc
"""Base model for Apilib db-mapped and virtual models.
"""
__mapper_args__ = {
# immediately fetch the value of server-generated default values after an INSERT or UPDATE
'eager_defaults': True,
}
File "../python2.7/site-packages/sqlalchemy/orm/query.py", line 2685, in _compile_context
"No column-based properties specified for "
InvalidRequestError: No column-based properties specified for refresh operation. Use session.expire() to reload collections and related items.
I had observed this issue after my 0.9 based fixes and that should have been fixed, what SQLA version are you using ?
Victor Varvariuc
2013-10-17 18:23:19 UTC
Permalink
I am using sqla 0.8
Post by Victor Varvariuc
"""Base model for Apilib db-mapped and virtual models.
"""
__mapper_args__ = {
# immediately fetch the value of server-generated default values
after an INSERT or UPDATE
'eager_defaults': True,
}
Looks like it works, but in other models without columns with
File "../python2.7/site-packages/sqlalchemy/orm/query.py", line 2685, in _compile_context
"No column-based properties specified for "
InvalidRequestError: No column-based properties specified for refresh
operation. Use session.expire() to reload collections and related items.
I had observed this issue after my 0.9 based fixes and that should have
been fixed, what SQLA version are you using ?
--
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 sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Michael Bayer
2013-10-17 19:10:04 UTC
Permalink
eager_defaults wasn't much of an option until 0.9, might have to work around that issue for now.
Post by Victor Varvariuc
I am using sqla 0.8
Post by Victor Varvariuc
"""Base model for Apilib db-mapped and virtual models.
"""
__mapper_args__ = {
# immediately fetch the value of server-generated default values after an INSERT or UPDATE
'eager_defaults': True,
}
File "../python2.7/site-packages/sqlalchemy/orm/query.py", line 2685, in _compile_context
"No column-based properties specified for "
InvalidRequestError: No column-based properties specified for refresh operation. Use session.expire() to reload collections and related items.
I had observed this issue after my 0.9 based fixes and that should have been fixed, what SQLA version are you using ?
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...