On Nov 4, 2009, at 7:33 PM, exhuma.twn wrote:

>
> Hi,
>
> All the docs only explain how to do inheritance using a simple "parent
>> child" model. But what about "parent > child > grandchild"? I tried
> the following but I get an error:
>
> <code>
>
> thing_table = Table( 'thing', metadata,
>      Column( 'uuid', String(36), primary_key=True,
> default=func.uuid_generate_v1() ),
>      Column( 'reporter', String(30), nullable=False )
>      Column( 'type', String(30), nullable=False )
>      )
>
> mineral_table = Table('mineral', metadata,
>    Column('uuid', String(36), primary_key=True, ForeignKey
> ('thing.uuid')),
>    Column( 'family', String(30), nullable=False )
> );
>
> crystal_table = Table( 'crystal', metadata,
>        Column('uuid', String(36), ForeignKey('mineral.uuid')),
>        Column('label', String(30)))
>
> class Thing(object): pass
> class Mineral(Thing): pass
> class Crystal(Mineral): pass
>
> mapper( Thing, thing_table, polymorphic_on=thing_table.c.type,
> polymorphic_identity='thing' )
> mapper(Mineral, mineral_table, inherits=Thing,
> polymorphic_identity='mineral')
> mapper(Crystal, crystal_table, inherits=Mineral,
> polymorphic_identity='crystal')
>
> </code>
>
> When I create the mappings like this, the model loads correctly, but I
> cannot create "Crystal" instances:

I think you need a primary key on crystal_table for it to be an  
official member of the joined table inheritance club.  Also 0.4.6 is a  
very old SQLA version and you'd have a lot more query fluency if you  
were on 0.5 at least.



>
>>>> x = Crystal()
>>>> x.family='kjdfh'
>>>> session.save(x)
>>>> session.flush()
>
> 2009-11-05 01:21:32,795 sqlalchemy.engine.base.Engine.0x..2c INFO
> BEGIN
> 2009-11-05 01:21:32,801 sqlalchemy.engine.base.Engine.0x..2c INFO
> SELECT uuid_generate_v1() AS uuid_generate_v1_1
> 2009-11-05 01:21:32,801 sqlalchemy.engine.base.Engine.0x..2c INFO {}
> 2009-11-05 01:21:32,805 sqlalchemy.engine.base.Engine.0x..2c INFO
> INSERT INTO thing (uuid, type) VALUES (%(uuid)s, %(type)s)
> 2009-11-05 01:21:32,806 sqlalchemy.engine.base.Engine.0x..2c INFO
> {'type': 'rock', 'uuid': '2bb7593c-c9a1-11de-8367-001d720c19db'}
> 2009-11-05 01:21:32,814 sqlalchemy.engine.base.Engine.0x..2c INFO
> INSERT INTO mineral (uuid, family) VALUES (%(uuid)s, %(family)s)
> 2009-11-05 01:21:32,815 sqlalchemy.engine.base.Engine.0x..2c INFO
> {'uuid': '2bb7593c-c9a1-11de-8367-001d720c19db', 'family': 'kjdfh'}
> 2009-11-05 01:21:32,896 sqlalchemy.engine.base.Engine.0x..2c INFO
> COMMIT
>
> Oddly, the "Crystal" instance is never saved. Any ideas? I am running
> this using SQLAlchemy 0.4.6.
> >


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