Hi,

First I need to let you know I do everything declarative......

To make things understandable I have chosen to use objects a bit more close to 
real-life.


There is a base object Person with a one-to-many relationship to the table 
addresses:

class Person(Base):
__tablename__           =       "person"
Id                                      = Column(Integer, primary_key=True)
.....
Addresses           = relation('Address',primaryjoin='Address.Person_Id == 
Person.Id', cascade="all")
.... 

Address is a polymorphic base type:

class Address(Base):
        __tablename__           = "adresses"
        Id                                      = Column(Integer, 
primary_key=True)
        discriminator                   = Column(Unicode(20))   
        __mapper_args__ = {'Polymorphic_on': discriminator}
        ....
        ...

for a lot of address types to be appended to Person.Addresses like:

class EmailAddress(Address):
    __tablename__       = "emailaddresses"
    __mapper_args__     = {'polymorphic_identity' : u'emailaddress'} 
    Id                  = Column(Integer,ForeignKey('addresses.Id'), 
primary_key=True)
    Value               = Column(Unicode(100))

class MSNAddress(Address):
    __tablename__       = "msnaddresses"
    __mapper_args__     = {'polymorphic_identity' : u'msnaddress'} 
    Id                  = Column(Integer,ForeignKey('addresses.Id'), 
primary_key=True)

class PostalAddress(Address):
    __tablename__       = "postaladdresses"
    Country             = Column(Unicode(2),primary_key=True)           
                                # Should "hold" the polymorphic Identity for 
subclass like "NL","D","UK", "US"

    __mapper_args__     = {'polymorphic_identity' : u'postaladdress'}    
                                # cannot add {'Polymorphic_on' : Counrty} to 
this.... see below in text what happens if I do

    Id                  = Column(Integer,ForeignKey('addresses.Id'), 
primary_key=True)




Now I want to make say a Dutch postaladdress, a german postaladdress.
The obvious way to do so is make the base class Polymorphic again but it will 
"overwrite" the __mapper_args__ of the base class Address

class NLPostalAddress(PostalAddress):
    __tablename__       = "nlpostaladdress"
    __mapper_args__     = {'polymorphic_identity' : u'NL'} # will "overwrite" 
the polymorphic Identity of PostalAddress base class
    Id                  = Column(Integer,ForeignKey('postaladdresses.Id'), 
primary_key=True)
    Street              = Column(Unicode(100))
    HouseNumber         = Column(Unicode(10))
    ...
    ...


Person.Addresses.append(NLPostalAddress(Street = "somestreet", HouseNum 
......)) works but with: Address. discriminator stays empty. and 
PostallAddress. Country will become 'NL'. 
Which is logical (look at the comments in the "code") since the polymorphic_on 
: Country makes the mapper "forget" it was polymorphic on 
"Address.discriminator"

PostalAddress.Id makes the ID part work well......

Is there a way to "nest" polymorph classes in some way so the above works... 

One solution might be Concrete tables but I have not been able to get that 
working either? since that would involve a "polymorphic union" I might be 
missing something there... but I do not really understand the documentation 
(I'm still using 0.5.8, upgrading is possible)

Again this is a "fake" data model to make my point

Any suggestions?


Martijn





-- 
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