you need to name the "secondary" table as it's name in the MetaData collection, 
not the variable name you've assigned it to, when configuring with string 
arguments.


On Mar 11, 2014, at 6:50 PM, Bala <[email protected]> wrote:

> Hi Michael, thanks for the prompt reply.
> 
> However, When I tried according to the suggestion, I get this error:
> 
>     45    class Groups(ModelBase, VersionColumnMixin):
>     46        domain_id = sa.Column(sa.String(36), DefaultClause('default'), 
> nullable=False)
>     47        description = sa.Column(sa.Text)
>     48        date_created = sa.Column(sa.DateTime)
>     49        group_name = sa.Column(sa.String(64), nullable=False, 
> index=True)
>     50        child_groups = relationship('Groups', 
> secondary=groups_and_groups_assoc_table,
>     51                                      
> primaryjoin="Groups.id==groups_and_groups_assoc_table.c.parent_group_id",
>     52                                      
> secondaryjoin="Groups.id==groups_and_groups_assoc_table.c.child_group_id",
>     53                                      backref='parent_groups')
>     54        __table_args__ = (sa.UniqueConstraint('domain_id', 
> 'group_name'),)
> 
> Traceback (most recent call last):
>   File "./selfref4.py", line 67, in <module>
>     g1 = Groups(description='1234', group_name='group1')
>   File "<string>", line 2, in __init__
>   File 
> "/usr/lib64/python2.6/site-packages/sqlalchemy/orm/instrumentation.py", line 
> 322, in _new_state_if_none
>     state = self._state_constructor(instance, self)
>   File "/usr/lib64/python2.6/site-packages/sqlalchemy/util/langhelpers.py", 
> line 689, in __get__
>     obj.__dict__[self.__name__] = result = self.fget(obj)
> ...
> ...File 
> "/usr/lib64/python2.6/site-packages/sqlalchemy/ext/declarative/clsregistry.py",
>  line 275, in __call__
>     (self.prop.parent, self.arg, n.args[0], self.cls)
> sqlalchemy.exc.InvalidRequestError: When initializing mapper 
> Mapper|Groups|groups, expression 
> 'Groups.id==groups_and_groups_assoc_table.c.parent_group_id' failed to locate 
> a name ("name 'groups_and_groups_assoc_table' is not defined"). If this is a 
> class name, consider adding this relationship() to the <class 
> '__main__.Groups'> class after both dependent classes have been defined.
> 
> 
> 
> 
> On Tuesday, March 11, 2014 2:10:52 PM UTC-7, Michael Bayer wrote:
> 
> On Mar 11, 2014, at 3:50 PM, Bala <[email protected]> wrote:
> 
>>     45    class Groups(ModelBase, VersionColumnMixin):
>>     46        id = sa.Column('id', sa.Integer, Sequence('id_seq'), 
>> primary_key=True)
>>     47        domain_id = sa.Column(sa.String(36), DefaultClause('default'), 
>> nullable=False)
>>     48        description = sa.Column(sa.Text)
>>     49        date_created = sa.Column(sa.DateTime)
>>     50        group_name = sa.Column(sa.String(64), nullable=False, 
>> index=True)
>>     51        child_groups = relationship('Groups', 
>> secondary=groups_and_groups_assoc_table,
>>     52                                      
>> primaryjoin=id==groups_and_groups_assoc_table.c.parent_group_id,
>>     53                                      
>> secondaryjoin=id==groups_and_groups_assoc_table.c.child_group_id,
>>     54                                      backref='parent_groups')
> 
> here, when the definition of the Python identifier "id" within the Groups 
> class definition is removed, it is no longer valid to refer to the word "id" 
> within the "primaryjoin" and "secondaryjoin" conditions, as "id" is not 
> defined.  The reason it seems to be defined is that it is resolving to the 
> Python built in function "id".  Unfortunately the exception message isn't 
> being too smart here, interpreting "id" as just another literal value like 
> the number "3" so you don't see it, it wouldn't be a bad idea for SQLA to 
> detect this kind of thing but that is actually somewhat difficult as it is 
> valid for an expression to refer to a callable.
> 
> Since "id" is not yet defined within the class body of Groups you need to 
> specify conditions using a string:
> 
> class Groups(ModelBase, VersionColumnMixin):
>     domain_id = sa.Column(sa.String(36), DefaultClause('default'), 
> nullable=False)
>     description = sa.Column(sa.Text)
>     date_created = sa.Column(sa.DateTime)
>     group_name = sa.Column(sa.String(64), nullable=False, index=True)
>     child_groups = relationship('Groups', 
> secondary=groups_and_groups_assoc_table,
>                                   
> primaryjoin="Groups.id==groups_and_groups.c.parent_group_id",
>                                   
> secondaryjoin="Groups.id==groups_and_groups.c.child_group_id",
>                                   backref='parent_groups')
>     __table_args__ = (sa.UniqueConstraint('domain_id', 'group_name'),)
> 
> 
> i'm updating the document at 
> http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#self-referential-many-to-many-relationship
>  to include this form now.
> 
> 
> 
> 
> 
> 
> -- 
> 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.

-- 
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.

Reply via email to