[sqlalchemy] Re: Inheritance and self-relation in child

2008-06-05 Thread Michael Bayer


On Jun 5, 2008, at 11:18 AM, Tomer wrote:


 Hi,

 I have two tables: Image and PersistentImage, where the latter
 inherits the former. In addition, PersistentImage has a foreign key to
 itself (ie, all PersistentImage objects form a hierarchy among
 themselves). I haven't been able to get this to work - first it
 complained about wanting a primaryjoin, then it seemed to get confused
 between the parent/children relation among PersistentImage objects and
 the inheritance between PersistentImage and Image. Here are my
 mappers:

 mapper(
   Image,
   imageTable,
   polymorphic_on=imageTable.c.type,
   polymorphic_identity='Image'
   )

 mapper(
   PersistentImage,
   persistentImageTable,
   inherits=Image,
   polymorphic_identity='PersistentImage',
   properties={
   'children': relation(PersistentImage, backref='parent')
   })

 And here are the actual table definitions:

 imageTable = Table('Images', metadata,
   Column('id', Integer, primary_key=True),
   Column('name', String(256), nullable=False),
   Column('type', String(30), nullable=False)
   )

 persistentImageTable = Table('PersistentImages', metadata,
   Column('id', Integer, ForeignKey('Images.id'), primary_key=True),
   Column('parentId', Integer, ForeignKey('PersistentImages.id')),
   Column('userId', Integer, ForeignKey('Users.id'), nullable=False)
   )

here it is:

mapper(PersistentImage, persistentImageTable, inherits=Image,  
inherit_condition=persistentImageTable.c.id==imageTable.c.id,
polymorphic_identity='PersistentImage',
   properties={
 'children':relation(PersistentImage,  
primaryjoin=persistentImageTable.c.parentId==persistentImageTable.c.id,
backref=backref('parent',  
primaryjoin 
=persistentImageTable.c.parentId==persistentImageTable.c.id,  
remote_side=[persistentImageTable.c.id])
)
  })

SQLA would probably slightly happier if you had the parentId foreign  
key referencing Images.id instead of PersistentImages.id but this  
should not be required.





--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Inheritance and self-relation in child

2008-06-05 Thread Tomer

Worked like magic!! Thanks!

BTW, why wasn't SQLA able to determine this automatically like it
usually does?

On Jun 5, 11:30 am, Michael Bayer [EMAIL PROTECTED] wrote:
 On Jun 5, 2008, at 11:18 AM, Tomer wrote:





  Hi,

  I have two tables: Image and PersistentImage, where the latter
  inherits the former. In addition, PersistentImage has a foreign key to
  itself (ie, all PersistentImage objects form a hierarchy among
  themselves). I haven't been able to get this to work - first it
  complained about wanting a primaryjoin, then it seemed to get confused
  between the parent/children relation among PersistentImage objects and
  the inheritance between PersistentImage and Image. Here are my
  mappers:

  mapper(
     Image,
     imageTable,
     polymorphic_on=imageTable.c.type,
     polymorphic_identity='Image'
     )

  mapper(
     PersistentImage,
     persistentImageTable,
     inherits=Image,
     polymorphic_identity='PersistentImage',
     properties={
             'children': relation(PersistentImage, backref='parent')
     })

  And here are the actual table definitions:

  imageTable = Table('Images', metadata,
     Column('id', Integer, primary_key=True),
     Column('name', String(256), nullable=False),
     Column('type', String(30), nullable=False)
     )

  persistentImageTable = Table('PersistentImages', metadata,
     Column('id', Integer, ForeignKey('Images.id'), primary_key=True),
     Column('parentId', Integer, ForeignKey('PersistentImages.id')),
     Column('userId', Integer, ForeignKey('Users.id'), nullable=False)
     )

 here it is:

 mapper(PersistentImage, persistentImageTable, inherits=Image,  
 inherit_condition=persistentImageTable.c.id==imageTable.c.id,
     polymorphic_identity='PersistentImage',
    properties={
      'children':relation(PersistentImage,  
 primaryjoin=persistentImageTable.c.parentId==persistentImageTable.c.id,
         backref=backref('parent',  
 primaryjoin
 =persistentImageTable.c.parentId==persistentImageTable.c.id,  
 remote_side=[persistentImageTable.c.id])
     )
   })

 SQLA would probably slightly happier if you had the parentId foreign  
 key referencing Images.id instead of PersistentImages.id but this  
 should not be required.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Inheritance and self-relation in child

2008-06-05 Thread Michael Bayer


On Jun 5, 2008, at 11:42 AM, Tomer wrote:


 Worked like magic!! Thanks!

 BTW, why wasn't SQLA able to determine this automatically like it
 usually does?

when you join PersistentImage-PersistentImage, theres two ways to  
join on foreign keys between those (remember that a PersistentImage is  
also an Image).  So SQLA reports that this is ambiguous.




 On Jun 5, 11:30 am, Michael Bayer [EMAIL PROTECTED] wrote:
 On Jun 5, 2008, at 11:18 AM, Tomer wrote:





 Hi,

 I have two tables: Image and PersistentImage, where the latter
 inherits the former. In addition, PersistentImage has a foreign  
 key to
 itself (ie, all PersistentImage objects form a hierarchy among
 themselves). I haven't been able to get this to work - first it
 complained about wanting a primaryjoin, then it seemed to get  
 confused
 between the parent/children relation among PersistentImage objects  
 and
 the inheritance between PersistentImage and Image. Here are my
 mappers:

 mapper(
Image,
imageTable,
polymorphic_on=imageTable.c.type,
polymorphic_identity='Image'
)

 mapper(
PersistentImage,
persistentImageTable,
inherits=Image,
polymorphic_identity='PersistentImage',
properties={
'children': relation(PersistentImage, backref='parent')
})

 And here are the actual table definitions:

 imageTable = Table('Images', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(256), nullable=False),
Column('type', String(30), nullable=False)
)

 persistentImageTable = Table('PersistentImages', metadata,
Column('id', Integer, ForeignKey('Images.id'), primary_key=True),
Column('parentId', Integer, ForeignKey('PersistentImages.id')),
Column('userId', Integer, ForeignKey('Users.id'), nullable=False)
)

 here it is:

 mapper(PersistentImage, persistentImageTable, inherits=Image,
 inherit_condition=persistentImageTable.c.id==imageTable.c.id,
 polymorphic_identity='PersistentImage',
properties={
  'children':relation(PersistentImage,
 primaryjoin 
 =persistentImageTable.c.parentId==persistentImageTable.c.id,
 backref=backref('parent',
 primaryjoin
 =persistentImageTable.c.parentId==persistentImageTable.c.id,
 remote_side=[persistentImageTable.c.id])
 )
   })

 SQLA would probably slightly happier if you had the parentId  
 foreign
 key referencing Images.id instead of PersistentImages.id but this
 should not be required.
 


--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Inheritance and self-relation in child

2008-06-05 Thread Tomer

Right, I missed that part.

Thanks for the explanation...

On Jun 5, 12:02 pm, Michael Bayer [EMAIL PROTECTED] wrote:
 On Jun 5, 2008, at 11:42 AM, Tomer wrote:



  Worked like magic!! Thanks!

  BTW, why wasn't SQLA able to determine this automatically like it
  usually does?

 when you join PersistentImage-PersistentImage, theres two ways to  
 join on foreign keys between those (remember that a PersistentImage is  
 also an Image).  So SQLA reports that this is ambiguous.



  On Jun 5, 11:30 am, Michael Bayer [EMAIL PROTECTED] wrote:
  On Jun 5, 2008, at 11:18 AM, Tomer wrote:

  Hi,

  I have two tables: Image and PersistentImage, where the latter
  inherits the former. In addition, PersistentImage has a foreign  
  key to
  itself (ie, all PersistentImage objects form a hierarchy among
  themselves). I haven't been able to get this to work - first it
  complained about wanting a primaryjoin, then it seemed to get  
  confused
  between the parent/children relation among PersistentImage objects  
  and
  the inheritance between PersistentImage and Image. Here are my
  mappers:

  mapper(
     Image,
     imageTable,
     polymorphic_on=imageTable.c.type,
     polymorphic_identity='Image'
     )

  mapper(
     PersistentImage,
     persistentImageTable,
     inherits=Image,
     polymorphic_identity='PersistentImage',
     properties={
             'children': relation(PersistentImage, backref='parent')
     })

  And here are the actual table definitions:

  imageTable = Table('Images', metadata,
     Column('id', Integer, primary_key=True),
     Column('name', String(256), nullable=False),
     Column('type', String(30), nullable=False)
     )

  persistentImageTable = Table('PersistentImages', metadata,
     Column('id', Integer, ForeignKey('Images.id'), primary_key=True),
     Column('parentId', Integer, ForeignKey('PersistentImages.id')),
     Column('userId', Integer, ForeignKey('Users.id'), nullable=False)
     )

  here it is:

  mapper(PersistentImage, persistentImageTable, inherits=Image,
  inherit_condition=persistentImageTable.c.id==imageTable.c.id,
      polymorphic_identity='PersistentImage',
     properties={
       'children':relation(PersistentImage,
  primaryjoin
  =persistentImageTable.c.parentId==persistentImageTable.c.id,
          backref=backref('parent',
  primaryjoin
  =persistentImageTable.c.parentId==persistentImageTable.c.id,
  remote_side=[persistentImageTable.c.id])
      )
    })

  SQLA would probably slightly happier if you had the parentId  
  foreign
  key referencing Images.id instead of PersistentImages.id but this
  should not be required.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---