[sqlalchemy] how to make many-to-many links against the same class with an association table

2010-09-30 Thread Julien Iguchi-Cartigny
Hi,

I've two classes CourseSet and CanonicalCourse (with the same parent)
which need to be connected in a many-to-many relationship.  But these
classes are mapped on the same table,  thus definition of the
association table seems redondant:

assoc_course_set_canonical_table = Table(
'CM_COURSE_SET_CANON_ASSOC_T', Base.metadata,
Column('CANON_COURSE', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.MEMBER_CONTAINER_ID')),
Column('COURSE_SET', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.MEMBER_CONTAINER_ID'))
)

But i can't define the relationship between. I've the error:

Could not determine join condition between parent/child tables on
relationship CourseSet.canonicalCourses.  Specify a 'primaryjoin'
expression.  If 'secondary' is present, 'secondaryjoin' is needed as
well.

I must have miss something: i've tried several possibilities with
primaryjoin and secondaryjoin but nothing work. Is it because i'm
refering at the same foreign key in the associative table ?

Thank you.

Julien.

PS: The code for my two classes ('#' comments the two lines with the problem)

class CourseSet(AbstractContainer):
parent_id = Column('PARENT_COURSE_SET', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.ENTERPRISE_ID'))
children = relationship(CourseSet, backref=backref('parent',
remote_side=AbstractContainer.eid))
#canonicalCourses = relationship(CanonicalCourse,
#secondary=assoc_course_set_canonical_table)

@classproperty
def __mapper_args__(self):
args = dict()
args.update(AbstractContainer.__mapper_args__)
args.update({'polymorphic_identity':
'org.sakaiproject.coursemanagement.impl.CourseSetCmImpl'})
return args

class CanonicalCourse(AbstractContainer):

@classproperty
def __mapper_args__(self):
args = dict()
args.update(AbstractContainer.__mapper_args__)
args.update({'polymorphic_identity':
'org.sakaiproject.coursemanagement.impl.CanonicalCourseCmImpl'})
return args

PS: the parents of these classes:

class AbstractPersistent(object):
version = Column('VERSION', Integer)
last_modified_by = Column('LAST_MODIFIED_BY', String(255))
last_modified_date = Column('LAST_MODIFIED_DATE', Date)
created_by = Column('CREATED_BY', String(255))
created_date = Column('CREATED_DATE', Date)

class AbstractNamed(AbstractPersistent):
eid = Column('ENTERPRISE_ID', String(255))
title = Column('TITLE', String(255))
description = Column('DESCRIPTION', String(255))

class AbstractContainer(Base,AbstractNamed):
__tablename__ = 'CM_MEMBER_CONTAINER_T'
id = Column('MEMBER_CONTAINER_ID',Integer,primary_key=True)
discriminator = Column('CLASS_DISCR', String(100))
__mapper_args__ = {'polymorphic_on': discriminator }



-- 
Trouble-a-cat limited

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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.



Re: [sqlalchemy] how to make many-to-many links against the same class with an association table

2010-09-30 Thread Michael Bayer
you need to specify primaryjoin and secondary join when you do self ref using 
m2m.   there's an example at the end of 
http://www.sqlalchemy.org/docs/orm/relationships.html#specifying-alternate-join-conditions-to-relationship
 , as well as some variants of that syntax which declarative introduces.



On Sep 30, 2010, at 3:15 PM, Julien Iguchi-Cartigny wrote:

 Hi,
 
 I've two classes CourseSet and CanonicalCourse (with the same parent)
 which need to be connected in a many-to-many relationship.  But these
 classes are mapped on the same table,  thus definition of the
 association table seems redondant:
 
 assoc_course_set_canonical_table = Table(
'CM_COURSE_SET_CANON_ASSOC_T', Base.metadata,
Column('CANON_COURSE', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.MEMBER_CONTAINER_ID')),
Column('COURSE_SET', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.MEMBER_CONTAINER_ID'))
 )
 
 But i can't define the relationship between. I've the error:
 
 Could not determine join condition between parent/child tables on
 relationship CourseSet.canonicalCourses.  Specify a 'primaryjoin'
 expression.  If 'secondary' is present, 'secondaryjoin' is needed as
 well.
 
 I must have miss something: i've tried several possibilities with
 primaryjoin and secondaryjoin but nothing work. Is it because i'm
 refering at the same foreign key in the associative table ?
 
 Thank you.
 
 Julien.
 
 PS: The code for my two classes ('#' comments the two lines with the problem)
 
 class CourseSet(AbstractContainer):
parent_id = Column('PARENT_COURSE_SET', Integer,
ForeignKey('CM_MEMBER_CONTAINER_T.ENTERPRISE_ID'))
children = relationship(CourseSet, backref=backref('parent',
remote_side=AbstractContainer.eid))
 #canonicalCourses = relationship(CanonicalCourse,
 #secondary=assoc_course_set_canonical_table)
 
@classproperty
def __mapper_args__(self):
args = dict()
args.update(AbstractContainer.__mapper_args__)
args.update({'polymorphic_identity':
'org.sakaiproject.coursemanagement.impl.CourseSetCmImpl'})
return args
 
 class CanonicalCourse(AbstractContainer):
 
@classproperty
def __mapper_args__(self):
args = dict()
args.update(AbstractContainer.__mapper_args__)
args.update({'polymorphic_identity':
'org.sakaiproject.coursemanagement.impl.CanonicalCourseCmImpl'})
return args
 
 PS: the parents of these classes:
 
 class AbstractPersistent(object):
version = Column('VERSION', Integer)
last_modified_by = Column('LAST_MODIFIED_BY', String(255))
last_modified_date = Column('LAST_MODIFIED_DATE', Date)
created_by = Column('CREATED_BY', String(255))
created_date = Column('CREATED_DATE', Date)
 
 class AbstractNamed(AbstractPersistent):
eid = Column('ENTERPRISE_ID', String(255))
title = Column('TITLE', String(255))
description = Column('DESCRIPTION', String(255))
 
 class AbstractContainer(Base,AbstractNamed):
__tablename__ = 'CM_MEMBER_CONTAINER_T'
id = Column('MEMBER_CONTAINER_ID',Integer,primary_key=True)
discriminator = Column('CLASS_DISCR', String(100))
__mapper_args__ = {'polymorphic_on': discriminator }
 
 
 
 -- 
 Trouble-a-cat limited
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalch...@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 sqlalch...@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.