Hi Mike,
Thanks for your help with my other problem. I have a couple of other
questions / a wishlist:
Is there a declarative way to add foreign keys to the history table when
using history_meta.py? In the app I'm making the user can view old versions
of a document, and I want to make sure e.g. the user that created the old
version can't be deleted while their old versions exist.
Finally, it would be nice if other attributes could be carried over to the
history mapper. If they had a similar API, the history objects could be
worked with (e.g. serialised to JSON) in the same way as the non-history
objects. Perhaps this could be done by carrying over methods decorated with
@declared_attr? Something like this (untested):
def test_history_relationships(self):
class User(self.Base):
__tablename__ = 'appuser'
id = Column(Integer, primary_key=True)
name = Column(String)
class Document(Versioned, self.Base):
__tablename__ = 'document'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("appuser.id"))
contents = Column(String)
@declared_attr
def user(cls):
return relationship(AppUser)
self.create_tables()
user = User(name="Fred")
self.session.flush()
document = Document(user_id=user.id)
document.contents = "foo"
self.session.flush()
document.contents = "bar"
self.session.flush()
DocumentHistory = Document.__history_mapper__.class_
v2 = self.session.query(Document).one()
v1 = self.session.query(DocumentHistory).one()
self.assertEqual(v2.user.name, "Fred")
self.assertTrue(hasattr(v1, 'user'))
self.assertEqual(v1.user.name, "Fred")
Although it would be nice if it worked with other attributes too, such as
hybrid properties. Maybe they would need to be specially decorated for use
only by the history mapper, so that relationships weren't made
bidirectional.
Currently I'm doing this by manually querying the related tables - but I
feel this is prone to error, because code for doing the join is duplicated.
E.g. instead of doing *user_name = v1.user.name*, I do:
user = self.session.query(User).filter_by(id=v1.user_id).one()
user_name = user.name
I feel like I'm doing something wrong; any suggestions for improving this
would be appreciated.
Cheers,
Alex
SQLAlchemy 1.0.8, Python 3, PostgreSQL 9.4
--
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.