On Jan 17, 2014, at 12:05 PM, Richard Gerd Kuesters <[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.


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to