because the join condition is on two primary key columns, the fact that
they are primary keys is overriding the fact that one of them is a foreign
key and its determining the direction of the relationship improperly.

if you give it a clue as to which side of the join is the "foreignkey"
like this, it works:

Person.mapper = mapper(Person, people,
              properties = {
        'Sites' : relation(PersonSite.mapper, uselist=True,
             primaryjoin=(peoplesites.c.person==people.c.key),
foreignkey=peoplesites.c.person),
      },
    )


however, the SA function that does this, _find_dependent() inside of
sqlalchemy/mapping/properties.py, should probably be fixed to detect this
condition better, so I added ticket #151 for that.


Dimi Shahbaz wrote:
> Hi,
> My abridged schema:
>
>
> people = Table("people", pg_engine,
>    Column('key', String, primary_key=True),
>    Column('firstname', String),
>    Column('lastname', String),
> )
>
> peoplesites = Table("peoplesites", pg_engine,
>     Column('person', String, ForeignKey("people.key"),
> primary_key=True),
>     Column('site', String),
> )
>
> PersonSite.mapper = mapper(PersonSite, peoplesites)
>
> Person.mapper = mapper(Person, people,
>               properties = {
>       'Sites' : relation(PersonSite.mapper, uselist=True,
>       primaryjoin=(peoplesites.c.person==people.c.key)),
>       },
>     )
>
>
> I think that's enough to show my problem.  Anyway, I have a method in
> the Person class called addSite() which creates a PersonSite() and
> does something like: self.Sites.append(newPersonSite).  When I go to
> do a commit, however, it seems that the INSERTs come in the wrong
> order: the peoplesites insert is done first.  This fails, because the
> 'person' field of the peoplesites table is a foreign key of people.key.
>
> My workaround is to do:
> objectsore.commit(newPerson)  #Get the person inserted
> objectsore.commit()  #Get everything else inserted
>
> I know SA is smart about these things, so I'm probably doing
> something wrong to cause this.  What could be the reason?
>
>
>
>
> --
> Dimi Shahbaz, Software Engineer Specialist
> California PASS Program
> www.cyberhigh.fcoe.k12.ca.us
>
>
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Sqlalchemy-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
>



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to