On Sep 10, 2014, at 5:11 PM, Anton <anschat...@gmail.com> wrote:

> So I am trying to do the following:
> class SimpleModel(object):
>     __table_args__ = {'extend_existing': True}
> 
>     id = db.Column(db.Integer, primary_key=True)
> 
> class GroupEntityAttributes(SimpleModel, db.Model):
>     __table_args__ = {'extend_existing': True}
>     __tablename__ = 'group_entity'
> 
>     #Table Columns
>     id = db.Column(db.Integer, primary_key=True)
>     group_id = db.Column(db.Integer, db.ForeignKey('groups.id'), 
> nullable=False)
>     entity_id = db.Column(db.Integer, db.ForeignKey('entities.id'), 
> nullable=False)
>     key = db.Column(db.String)
>     value = db.Column(db.Text)
> 
>     #Relationships
>     entity = db.relationship('Entity', lazy='joined')
>     group = db.relationship('Group')
> 
> class GroupEntityAttributesPoly(GroupEntityAttributes):
>     __mapper_args__ = {'polymorphic_on': GroupEntityAttributes.key}
> 
> class GroupEntity(GroupEntityAttributesPoly):
>     __mapper_args__ = {'polymorphic_identity': '_has_connection'}
> 
>     others = db.relationship(
>         'GroupEntityAttributes',
>         primaryjoin=and_(
>             remote(foreign(GroupEntityAttributes.group_id)) == 
> GroupEntityAttributes.group_id,
>             remote(foreign(GroupEntityAttributes.entity_id)) == 
> GroupEntityAttributes.entity_id,
>         )    
>     )
> 
> 

What's illustrated here doesn't really make sense.  If 
GroupEntityAttributes.group_id referred to "groups.id", then a relationship 
extending from GroupEntity would need to refer to whatever class is mapped to 
the "entities" table, which I don't see here.  It looks like you're taking 
foreign key columns on the "group_entity" table and somehow pointing it to 
itself in that primaryjoin which doesn't make sense.   GroupEntity would need 
to be mapped to either "entities" or "groups" in order to refer to 
GroupEntityAttributes using that pattern and this sort of looks like the 
association object pattern, see 
http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html#association-object 
for an example of what that looks like.

Other things that you probably don't want:

1. polymorphic_on on a subclass, that should always go on the mapped class at 
the bottom of the hierarchy, see 
http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html#joined-table-inheritance
 for basic examples

2. extend_existing, this is usually only relevant when you're using reflection, 
otherwise it probably means you're setting things up incorrectly 










> When I try to load the model GroupEntity I get an error:
> ArgumentError: Can't determine relationship direction for relationship 
> 'GroupEntity.others' - foreign key columns within the join condition are 
> present in both the parent and the child's mapped tables.  Ensure that only 
> those columns referring to a parent column are marked as foreign, either via 
> the foreign() annotation or via the foreign_keys argument.
> 
> I am not sure how to overcome this error. Would appreciate any help.
> 
> Best,
> Anton.
> 
> On Tuesday, September 9, 2014 1:10:40 PM UTC-7, Anton wrote:
> Trying to use with multiple table mapping.
> 
> On Tuesday, September 9, 2014 1:03:25 PM UTC-7, Anton wrote:
> Hi, 
> 
> I need some help choosing the right pattern for the models I have. I have the 
> following tables:
> Entity (INT id, VARCHAR name)
> Group (INT id, VARCHAT name)
> GroupEntityAttributes(INT entity_id, INT group_id, VARCHAR key, VARCHAR name)
> 
> Entity and Group models are pretty straight forward. But 
> GroupEntityAttributes represents Entity within some group with custom 
> attributes added, so it resembles this pattern ( 
> https://bitbucket.org/zzzeek/sqlalchemy/src/374173e89d4e21a75bfabd8a655d17c247b6f1fc/examples/vertical/dictlike.py?at=master
>  ), the only difference is that there is an additional foreign key.
> I wonder if there is any approach I can use to adapt vertical pattern for my 
> needs without modifying tables.
> 
> Best,
> Anton.
> 
> 
> 
> -- 
> 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to