Re: [sqlalchemy] nested inheritance / polymorphic relationships

2013-06-04 Thread Michael Bayer

On Jun 4, 2013, at 1:55 AM, Amir Elaguizy aelag...@gmail.com wrote:

 Hi there,
 
 I have a tree that looks like this, reflected via polymorphic inheritance:

what do we mean reflected here, are you reflecting tables from the database, 
that is, 
http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html#metadata-reflection ?   

 
 That works great, like:
 
 
 class BaseModel(db.Model): # Table A in diagram
 __tablename__ = entities
 
 id = db.Column(db.BigInteger, primary_key=True, nullable=False, 
 server_default=func.nextval('guid_seq'))
 type_id = db.Column(db.SmallInteger, db.ForeignKey(EntityTypesModel.id))
 
 __mapper_args__ = {
 'polymorphic_identity':'entity',
 'polymorphic_on':type_id,
 'with_polymorphic':'*'
 }

this is why I question the word reflected because I don't see you using 
reflection there.  

 
 class BrandModel(BaseModel):   # Table B, C, D in diagram
 __tablename__ = 'brands'
 
 id = db.Column(db.BigInteger, db.ForeignKey(StufffModel.id), 
 primary_key=True, nullable=False)
 name = db.Column(db.String, nullable=False)
 
 __mapper_args__ = {
 'polymorphic_identity':ET_BRAND,
 }

Im confused by this as well - are you saying that you map the same class to B, 
C, and D rows?   That would be unusual.  It wouldn't work at all on the 
persistence side as SQLAlchemy could not know which of B, C, or D you wish for 
a particular BrandModel to be persisted towards.


 
 
 The problem is I need to reflect something more like this:
 
  A
   /   |   \
 B   C   D
  /   \
EF
 
 Where D is not only a polymorphic child of A but also the polymorphic parents 
 of E  F.
 
 It seems like I have to choose, D can either be a polymorphic child or it can 
 be a parent - it can't be both.
 
 Do I have any options here?

SQLAlchemy can represent inheritance hierarchies of any depth.However, 
because you are assigning a single subclass to all of B, C, and D that might be 
why there's an issue here, you'd need to assign a distinct subclass of 
BaseModel to at least D, and then another subclass of D_Model to handle E and 
F.

Preferably, you'd produce distinct classes for all six tables.



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




Re: [sqlalchemy] nested inheritance / polymorphic relationships

2013-06-04 Thread Amir Elaguizy
I didn't intend to use the word reflected in the Programming sense, I meant
in the traditional sense: is represented by.

That sentence was confusing, sorry!

I was saying class B,  C, and D are all defined using that same pattern.
They each have their own class.

Amir

On Tuesday, June 4, 2013, Michael Bayer wrote:


 On Jun 4, 2013, at 1:55 AM, Amir Elaguizy aelag...@gmail.comjavascript:;
 wrote:

  Hi there,
 
  I have a tree that looks like this, reflected via polymorphic
 inheritance:

 what do we mean reflected here, are you reflecting tables from the
 database, that is,
 http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html#metadata-reflection?

 
  That works great, like:
 
 
  class BaseModel(db.Model): # Table A in diagram
  __tablename__ = entities
 
  id = db.Column(db.BigInteger, primary_key=True, nullable=False,
 server_default=func.nextval('guid_seq'))
  type_id = db.Column(db.SmallInteger,
 db.ForeignKey(EntityTypesModel.id))
 
  __mapper_args__ = {
  'polymorphic_identity':'entity',
  'polymorphic_on':type_id,
  'with_polymorphic':'*'
  }

 this is why I question the word reflected because I don't see you using
 reflection there.

 
  class BrandModel(BaseModel):   # Table B, C, D in diagram
  __tablename__ = 'brands'
 
  id = db.Column(db.BigInteger, db.ForeignKey(StufffModel.id),
 primary_key=True, nullable=False)
  name = db.Column(db.String, nullable=False)
 
  __mapper_args__ = {
  'polymorphic_identity':ET_BRAND,
  }

 Im confused by this as well - are you saying that you map the same class
 to B, C, and D rows?   That would be unusual.  It wouldn't work at all on
 the persistence side as SQLAlchemy could not know which of B, C, or D you
 wish for a particular BrandModel to be persisted towards.


 
 
  The problem is I need to reflect something more like this:
 
   A
/   |   \
  B   C   D
   /   \
 EF
 
  Where D is not only a polymorphic child of A but also the polymorphic
 parents of E  F.
 
  It seems like I have to choose, D can either be a polymorphic child or
 it can be a parent - it can't be both.
 
  Do I have any options here?

 SQLAlchemy can represent inheritance hierarchies of any depth.However,
 because you are assigning a single subclass to all of B, C, and D that
 might be why there's an issue here, you'd need to assign a distinct
 subclass of BaseModel to at least D, and then another subclass of D_Model
 to handle E and F.

 Preferably, you'd produce distinct classes for all six tables.



 --
 You received this message because you are subscribed to a topic in the
 Google Groups sqlalchemy group.
 To unsubscribe from this topic, visit
 https://groups.google.com/d/topic/sqlalchemy/pI62wMDb6M4/unsubscribe?hl=en
 .
 To unsubscribe from this group and all its topics, send an email to
 sqlalchemy+unsubscr...@googlegroups.com javascript:;.
 To post to this group, send email to sqlalchemy@googlegroups.comjavascript:;
 .
 Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.




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




Re: [sqlalchemy] nested inheritance / polymorphic relationships

2013-06-04 Thread Ladislav Lenart
Hello.

 It seems like I have to choose, D can either be a polymorphic child or it can 
 be
 a parent - it can't be both.

 Do I have any options here?

I am almost sure you are correct. This is not possible in SA so you have to
flatten your hierarchy.

I don't know about experiences with inheritance of others on this list, but mine
in SA0.7.9 is not that good. I encountered various limitations along the way. I
guess it dependes on the complexity of the queries. We plan to get rid of it
eventually in our app. However, if I am not mistaken, SA0.8 addresses all the
quirks.


Ladislav Lenart


On 4.6.2013 07:55, Amir Elaguizy wrote:
 Hi there,
 
 I have a tree that looks like this, reflected via polymorphic inheritance:
 
 
  A
   /   |   \
 B   C   D
 
 That works great, like:
 
 
 class BaseModel(db.Model): # Table A in diagram
 __tablename__ = entities
 
 id = db.Column(db.BigInteger, primary_key=True, nullable=False,
 server_default=func.nextval('guid_seq'))
 type_id = db.Column(db.SmallInteger, db.ForeignKey(EntityTypesModel.id))
 
 __mapper_args__ = {
 'polymorphic_identity':'entity',
 'polymorphic_on':type_id,
 'with_polymorphic':'*'
 }
 
 class BrandModel(BaseModel):   # Table B, C, D in diagram
 __tablename__ = 'brands'
 
 id = db.Column(db.BigInteger, db.ForeignKey(StufffModel.id),
 primary_key=True, nullable=False)
 name = db.Column(db.String, nullable=False)
 
 __mapper_args__ = {
 'polymorphic_identity':ET_BRAND,
 }
 
 
 The problem is I need to reflect something more like this:
 
  A
   /   |   \
 B   C   D
  /   \
EF
 
 Where D is not only a polymorphic child of A but also the polymorphic parents 
 of
 E  F.
 
 It seems like I have to choose, D can either be a polymorphic child or it can 
 be
 a parent - it can't be both.
 
 Do I have any options here?
 
 -- 
 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 sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
 For more options, visit https://groups.google.com/groups/opt_out.

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