well, i have that feeling too. it's a basic NxN relationship between the same instance type. the anti-pattern might be having a polymorphic model to do that. i know sqlalchemy can handle this issue and provide me constant results, but by hand it can give me trouble ... should I remove the polymorphic association for logic' sake?


On 01/17/2014 03:24 PM, Michael Bayer wrote:

On Jan 17, 2014, at 12:05 PM, Richard Gerd Kuesters <[email protected] <mailto:[email protected]>> wrote:

hi all,

I'm having a tricky situation while modeling a database here. first, i'll just put the code of my two classes / models:

*
**class Resource(Base):**
**
**    resource_id = Column(Integer, Sequence, primary_key=True)**
** resource_type_id = Column(Integer, ForeignKey(SomeClass.id), nullable=False, index=True)**
**    p_discriminator = Column(Integer)**
**
**    __mapper_args__ = dict(**
**        polymorphic_on=p_discriminator,**
**        polymorphic_identity=1**
**    )**
**
**class ResourceRelationship(Resource):**
**
** resource_id = Column(Integer, ForeignKey(Resource.resource_id), primary_key=True, index=True)** ** left_resource_id = Column(Integer, ForeignKey(Resource.resource_id), nullable=True, index=True)** ** right_resource_id = Column(Integer, ForeignKey(Resource.resource_id), nullable=True, index=True)**
**
**    __mapper_args__ = dict(**
**        polymorphic_identity=2**
**        inherit_condition=(resource_id == Resource.resource_id)**
**    )*


ok, nothing fancy but, I would really like to create a unique constraint on ResourceRelationship, using left_resouce_id, right_resource_id AND the inherited resource_type_id.

the problem is: i can do that only using concrete inheritance, afaik. i would not like to do that because i really like the way polymorphic models work -- and the most important reason: this database is shared with other applications that would not rely on sqlalchemy to do the magic of maintain the data integrity.


you’d need to add resource_type_id to ResourceRelationship as well. The SQLAlchemy mapping will maintain the same value for both columns provided you map them under the same attribute name.

i have a sneaky feeling there’s some relational design antipattern occurring here but I’m not adept enough to determine specifically what that might be.



--
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/groups/opt_out.

Reply via email to