I am trying to create a generic "versioned" SQLAlchemy table model, from
which I can derive further models. Among other things, I need a
*is_latest_version* column property that can be used to create appropriate
joins…
Following code works fine:
class Versioned(db.Model):
__abstract__ = True
id = Column(Integer, primary_key=True)
version = Column(Integer, primary_key=True)
class Child(Versioned):
pass
t = aliased(Child, name="t")
Child.is_latest_version = db.column_property(
select([ Child.version == db.func.max(t.version) ])
.select_from(t)
.where(t.id == Child.id)
)
But I cannot figure any way to move the column property up to the parent
class. Any variation I attempt, bumps into class mapping issues…
Closest I could think off, would be something like:
class Versioned(db.Model):
__abstract__ = True
id = Column(Integer, primary_key=True)
version = Column(Integer, primary_key=True)
@declared_attr
def is_latest_version(cls):
t = aliased(cls, name="t")
return db.column_property(
select([ cls.version == db.func.max(t.version) ])
.select_from(t)
.where(t.id == cls.id)
)
class Child(Versioned):
pass
But it throws an error:
`sqlalchemy.orm.exc.UnmappedClassError: Class 'app.models.Child' is not
mapped`
Using quotes does not work either (query is executed with quoted fields as
literal strings).
Is there any way to achieve this kind of column_property in a virtual class?
Also… Not sure if that's related, but I also seem unable to implement
*is_latest_version* with a nicer join-based query (I suspect solving my
Alias issues might help):
… FROM child c1 JOIN child c2 ON c2.id = c1.id AND c2.version > c1.version
WHERE c2.id IS NULL
Thanks in advance for any help!
--
Dave
--
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.