Michael Bayer schrieb:
> in theory you should be able to do this:
> 
> 'groups':relation(Group, primaryjoin=pg_user.c.usesysid==func.any 
> (pg_group.c.grolist), viewonly=True)
> 
> notice the "viewonly" flag which is in the trunk only, which will  
> tell SA not to try persisting that mapping (since it cant)...i would  
> gather that was the problem you had if you had tried it this way before.

Without the "viewonly" flag, I got "No syncrules generated" errors.
These have disappeared, but there are still problems here.

Again, assume the following setup:

pg_user = Table('pg_user', metadata,
    Column('usesysid', Integer, primary_key=True),
    Column('usename', String, unique=True),
    Column('usesuper', Boolean))

pg_group = Table('pg_group', metadata,
    Column('grosysid', Integer, primary_key=True),
    Column('groname', String, unique=True),
    Column('grolist', String))

class User(object):
    pass

class Group(object):
    pass

Now when I map as follows:

mapper(User, pg_user, properties={
    'user_id': pg_user.c.usesysid,
    'user_name': pg_user.c.usename,
    'is_super': pg_user.c.usesuper,
    'groups': relation(Group, viewonly=True,
        primaryjoin=pg_user.c.usesysid==func.any(pg_group.c.grolist))})

I get this error:

sqlalchemy.exceptions.ArgumentError: On relation 'groups', can't figure
out which side is the foreign key for join condition 'pg_user.usesysid =
any(pg_group.grolist)'.  Specify the 'foreignkey' argument to the relation.

Shouldn't it be clear what the foreign key is in this situation?

Now when I explicitly specify the foreign key (as pg_group.grosysid or
pg_group.grolist, doesn't matter),

mapper(User, pg_user, properties={
    'user_id': pg_user.c.usesysid,
    'user_name': pg_user.c.usename,
    'is_super': pg_user.c.usesuper,
    'groups': relation(Group, viewonly=True,
        primaryjoin=pg_user.c.usesysid==func.any(pg_group.c.grolist),
        foreignkey=pg_group.c.grosysid)})

Then the groups property returns all existing groups, not the groups of
the corresponding user (the following query is echoed by the engine):

SELECT pg_group.grolist, pg_group.grosysid, pg_group.groname
FROM pg_user, pg_group
WHERE pg_user.usesysid = any(pg_group.grolist)
ORDER BY pg_group.grosysid

-- Christoph

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~----------~----~----~----~------~----~------~--~---

Reply via email to