yeah i have a secret weapon for that sort of thing, called  
"post_update"...you might be the first to use it.

this is a paraphrased version of how it would look, you have to fix  
up the join conditions/backrefs to suit your tables:

mapper(Page, pages, properties={
     'job': relation(Job, backref=backref('pages', cascade="all,  
delete-orphan")),
     'current_version' : relation(PageVersion,  
primaryjoin=pages.c.currentversion==pageversions.c.version,  
post_update=True),
     'versions' : relation(PageVersion,  
primaryjoin=pages.c.pagename==pageversions.c.pagename)
})

the idea of "post_update" is that its going to insert/update the row  
on the "pages" table normally, then do the "versions", and then do a  
*second* update on the "pages" table to get the "current_version"  
foreign key in there.

ive had unit tests in place for this for quite some time now but lets  
see if it works in a more complex scenario (if it doesnt work, then  
your test cases will become new unit tests).


On Jun 14, 2006, at 11:35 AM, Arnar Birgisson wrote:

> Hi all,
>
> I have a model with three classes, Job, Page and PageVersions. A job
> has many pages and a page has many versions.
>
> Now, I use a combined primary key for Page and PageVersions (see code
> below) - and have "normal" relations to each other  (job.pages,
> page.job, page.versions, pageversion.page etc.)
>
> Only one version of a page is "active", and is marked so with a
> boolean attribute on PageVersion. I would like to add a property on
> Page that points to that version (this would of course create a
> circular reference) - something like page.currentversion - with a
> corresponding Integer column "currentversion" on the pages table.
>
> Is this possible with SA given the kind of combined keys I'm using?
>
> The fallback solution is to do an eager-load on all pageversions
> attached to a page and handle this with a regular python property.
>
> Arnar
>
> jobs = Table("jobs", __meta__,
>                 Column("jobno", Unicode(15), primary_key=True),
>                 Column("created", DateTime, nullable=False,
> default=datetime.now))
>
> class Job(object):
>     pass
>
> mapper(Job, jobs)
>
> pages = Table("pages", __meta__,
>                 Column("jobno", Unicode(15), ForeignKey("jobs.jobno"),
> primary_key=True),
>                 Column("pagename", Unicode(30), primary_key=True),
>                 Column("created", DateTime, nullable=False,
> default=datetime.now))
>
> class Page(object):
>     pass
>
> mapper(Page, pages, properties={
>     'job': relation(Job, backref=backref('pages', cascade="all,  
> delete-orphan"))
> })
>
> pageversions = Table("pageversions", __meta__,
>                 Column("jobno", Unicode(15), primary_key=True,
> ForeignKey("pages.jobno")),
>                 Column("pagename", Unicode(30), primary_key=True,
> ForeignKey("pages.pagename")),
>                 Column("version", Integer, primary_key=True),
>                 Column("created", DateTime, nullable=False,
> default=datetime.now),
>                 Column("active", Boolean, nullable=False,  
> default=True))
>
> class PageVersion(object):
>     pass
>
> mapper(PageVersion, pageversions, properties={
>     'page': relation(Page,backref=backref('versions', cascade="all,
> delete-orphan",
>                                             lazy=False,
> order_by=pageversions.c.version))
> })
>
>
> _______________________________________________
> Sqlalchemy-users mailing list
> Sqlalchemy-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to