On Feb 8, 2012, at 9:39 AM, cropr wrote:

> I aam having an issue defining a 2nd foreign key constraint on 2
> derived classes of a base class that uses a self referential join.
> 
> Base class:ContentBase
> 
> class ContentBase(Base):
>    __tablename__ = 'content'
>    __mapper_args__ = {'polymorphic_on': ctype,
> 'polymorphic_identity':'contentbase'}
>    id = Column(types.Integer, primary_key=True, autoincrement=True)
>    children = relationship("ContentBase", backref=backref('parent',
> remote_side=id))
>    ....
> 
> Derived class CdRoot
> 
> class CdRoot(ContentBase):
>    __tablename__ = 'cdroot'
>    __mapper_args__ = {'polymorphic_identity' : 'cdroot'}
>    contentType = 'CdRoot'
>    id = Column(types.Integer, ForeignKey(ContentBase.id),
> primary_key=True)
>    clubs = relationship('CdClub', primaryjoin="cdroot.id ==
> cdclub.cdroot_id")
>    ...
> 
> Derived class CdClub
> 
> class CdClub(ContentBase):
>    __tablename__ = 'cdclub'
>    __mapper_args__ = {'polymorphic_identity' : 'cdclub'}
>    id = Column(types.Integer, ForeignKey(ContentBase.id),
> primary_key=True)
>    cdroot_id = Column(types.Integer, ForeignKey(CdRoot.id))
>    ...
> 
> The 2nd join between CdRoot and CdClub fails with the message : Can't
> determine join between content and
> cdclub, tables have more than one FK constraint.
> 
> Apparently the primaryjoin condition is not sufficient to get rid of
> the error.  Any clue how to solve this


Assuming this is paraphrasing since "cdroot.id==cdclub.cdroot_id" doesn't have 
the correct casing, also there's a relationship on ContentBase that doesn't 
appear to refer to any column but I assume that's under your "...".

Filling in the blanks with reasonable guesses it works fine:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy

Base= declarative_base()

class ContentBase(Base):
   __tablename__ = 'content'
   id = Column(Integer, primary_key=True, autoincrement=True)
   ctype = Column(String)
   parent_id = Column(Integer, ForeignKey('content.id'))
   children = relationship("ContentBase", 
backref=backref('parent',remote_side=id))
   __mapper_args__ = {'polymorphic_on': 
ctype,'polymorphic_identity':'contentbase'}


class CdRoot(ContentBase):
   __tablename__ = 'cdroot'
   __mapper_args__ = {'polymorphic_identity' : 'cdroot'}
   contentType = 'CdRoot'
   id = Column(Integer, ForeignKey(ContentBase.id),primary_key=True)
   clubs = relationship('CdClub', primaryjoin="CdRoot.id ==CdClub.cdroot_id")

class CdClub(ContentBase):
   __tablename__ = 'cdclub'
   __mapper_args__ = {'polymorphic_identity' : 'cdclub'}
   id = Column(Integer, ForeignKey(ContentBase.id),primary_key=True)
   cdroot_id = Column(Integer, ForeignKey(CdRoot.id))

configure_mappers()

if you still have problems, please provide a fully working example, thanks !




> 
> Ruben
> 
> -- 
> 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 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to