On May 20, 2010, at 10:20 AM, bogun.dmit...@gmail.com wrote:

> Hello.
> 
> Is there any way I can use some expression for polymorphic_on mapper 
> attribute?

You can get this behavior now with some effort if you create a SELECT statement 
that loads the full row, including your "discriminator", and set it as 
"with_polymorphic".   There's some examples for this in 
http://www.sqlalchemy.org/docs/mappers.html#concrete-table-inheritance .

Allowing "polymorphic_on" to just be a plain function, which would be eaiser, 
is a TODO, there's tickets in trac to address this feature eventually.




> 
> I have tried some ways, but not got what I want.
> 
> # --- my tries
> import sqlalchemy as sa
> from sqlalchemy import *
> from sqlalchemy import orm
> from sqlalchemy.ext.declarative import declarative_base
> 
> _decl = declarative_base()
> 
> class generic(_decl):
>     __tablename__ = 'generic'
>     
>     idnr      = Column(Integer, primary_key=True)
>     type      = Column(String(32), nullable=False)
>     data      = Column(String(128))
>     discriminator = orm.column_property(case(value=type, whens={'abc':type, 
> 'cde':type}, else_='generic').label('discriminator'))
> 
>     __mapper_args__ = {'polymorphic_on': type, 
> 'polymorphic_identity':'generic'}
>     #__mapper_args__ = {'polymorphic_on': discriminator, 
> 'polymorphic_identity':'generic'}
>     #__mapper_args__ = {'polymorphic_on': case(value=type, whens={'abc':type, 
> 'cde':type}, else_='generic'), 'polymorphic_identity':'generic'}
> 
> class type_abc(generic):
>     __tablename__ = 'abc'
> 
>     generic_idnr = Column(Integer, ForeignKey(generic.idnr, 
> onupdate='CASCADE', ondelete='CASCADE'), primary_key=True)
>     abc               = Column(Integer, nullable=False)
>     __mapper_args__ = {'polymorphic_identity':'abc'}
> 
> class type_cde(generic):
>     __tablename__ = 'cde'
> 
>     generic_idnr = Column(Integer, ForeignKey(generic.idnr, 
> onupdate='CASCADE', ondelete='CASCADE'), primary_key=True)
>     cde               = Column(Integer, nullable=False)
>     __mapper_args__ = {'polymorphic_identity':'cde'}
> 
> def main():
>     engine = create_engine('sqlite:///:memory:', echo=True)
>     generic.metadata.create_all(engine)
>     sm = orm.sessionmaker(bind=engine)
>     ses = sm()
> 
>     a = type_cde()
>     a.cde = 1
>     ses.add(a)
> 
>     a = type_abc()
>     a.abc = 2
>     ses.add(a)
> 
>     #pdb.set_trace()
>     a = generic()
>     a.type = 'zzz'
>     a.data = 'a vot tak?'
>     ses.add(a)
> 
>     ses.commit()
> 
> if __name__ == '__main__':
>     main()
> # ---- code end
> 
> It works only with real column instance... 
> 
> -- 
> 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.

Reply via email to