This whole thing is driving me crazy, What I want:


class Extra(Base):
        __tablename__                   = "extra"
        # Primary key consists of two different columns !!!
        tableId                                 = Column(Integer, 
primary_key=true)
        tablename                               = Column(Unicode(20), 
primary_key=True)

        info                                            = Column(........) #Not 
relevant




class ex1(Base):
    Id                  = Column(Integer, primary_key=True)
    discriminator       = Column(Unicode(20))

    @declared_attr
    def __tablename__(self):
        return self.__name__.lower()
    
    @declared_attr
    def __mapper_args__(self):
        if self.__name__ == 'ext1':
            return {'polymorphic_on': self.discriminator, 
                    'polymorphic_identity':unicode(self.__name__.lower()),
                    'extension': FilePropertiesMapperExtension(),
                    'batch' : False}
        else:
            return {'polymorphic_identity':unicode(self.__name__.lower()),
                    'inherit_condition': self.Id == extra.Id,                   
                        #needed for something else in this config (multiple 
self reference)
                    'extension': FilePropertiesMapperExtension(),               
                #Needed for something else, not relevant for this sample
                    'batch' : False}                                            
                                        # ,,                    ,,              
        ,,                      ,,


  # Set up foreignkey and relation to Extra....
    __table_args__      = (ForeignKeyConstraint(['discriminator', 'Id'], 
['extra.Table','extra.TableId']),{})


    extras                = relation('Extra', cascade="all", lazy="dynamic" 
backref="owner")


        ....
        ....


class ext2(ext1):
    Id                  = Column(Integer,ForeignKey('ext1.Id'), 
primary_key=True)
  ......


class ext3(ext1):
    Id                  = Column(Integer,ForeignKey('ext1.Id'), 
primary_key=True)
.....




Now I want:

Ext2 = ext2()
Extra_info = extra()
Ext2.extras.append(Extra_Info)


Ext2.discriminator should be "ext2"
Ext2.Id should be 1 for the first record

Extra_Info should be created in the database, with its columns : id set to the 
Ext2.id and tablename to Ext.discriminator ......
Extra_Info.owner would point to Ext2

If Ext2 is deleted, all related extrainfo record would be delete too

if one Extra_Info is deleted, Extra_Info.owner should stay in place as well as 
all other related 

Extra is many to one polymorhic version of ext1

I hope this clarifies more what I want....

I really need the @declared_attr way of doing stuff and that is not related to 
this question but might influence this question so I left it in......

Martijn


On Feb 10, 2011, at 18:13 , Michael Bayer wrote:

> 
> On Feb 10, 2011, at 4:20 AM, Martijn Moeling wrote:
> 
>> Another small thing:
>> 
>> I took a look at:
>> 
>> ForeignKeyConstraint(['invoice_id', 'ref_num'], ['invoice.invoice_id', 
>> 'invoice.ref_num'])
>> 
>> Now for Polymorphic tables:
>> 
>> 
>> in baseclass:
>> 
>> baseclass.discriminator happens to be the __tablename__ of the polymorphic 
>> 
>> ForeignKeyConstraint('['baseclass.disciminator', baseclass.Id'], 
>> ['someotherclass.tablename','someotherclass.tableId']
>> relationship('someotherclass', backref=baseclass, cascade="all", 
>> lazy="dynamic")
>> 
>> 
>> in someotheclass:
>>      
>>      tablename = column(Unicode(20), primary_key=True)
>>      tableId         = column(Integer, primary_key=True)
>> 
>> seems Ok to me.
>> 
>> Now I need to make someotherclass work with non-polymorphic tables too!!
>> 
>> anotherclass:
>>      Id = column(Integer, primary_key=True)
>>      
>> ForeignKeyConstaint('[anotherclass.__tablename__,'anotherclass.Id'],['someotherclass.tablename','someotherclass.tableId'])
>>      relation('someotherclass', backref=baseclass, cascade="all", 
>> lazy="dynamic")
>> 
>> Is there any way to get this working without configuring it as polymorphic 
>> an do no Inhiritance, I do not want each anotherclass record to have a 
>> column discriminator with its own tablename!
>> 
>> or can I use anotherclass.__tablename__ in the ForeignKeyConstaint?
>> 
>> This has to do with the someotherclass being the "ACL" I talked about in a 
>> previous post if that gives extra info. I am trying to implement the 
>> MapperExtension.before_append where I need to refer to the "ACL" records in 
>> a way like:
>> 
>> for A in instance.ACL:
>>      .....
> 
> yeah, sorry, this use case continues to be 98% opaque to me.   I don't 
> understand what you mean by "make someotherclass work with non-polymorphic 
> tables", a class is mapped in just one way, either with or without a 
> discriminator column.  A single class can't be mapped in both ways.    If 
> there's no discriminator, there's just one class that can be used for 
> returned rows.
> 
> If you could create a small test that illustrates a mapping and an expected 
> result, perhaps I can attempt to find a way to get the ORM behavior you're 
> looking for.
> 
> Your mapper extension would continue to be used normally with a "dynamic" 
> relationship since it uses query(cls) just like a regular query.
> 
> 
> 
> -- 
> 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