Em Qui, 2008-01-10 às 21:20 -0500, Rick Morrison escreveu:
> You're mixing single-table inheritance (using the discriminator
> column), with concrete inheritance (using multiple tables). 
> 
> You have to pick one scheme or the other. Either use a single
> inheritance chain, or separate the two class hierarchies into two
> separate chains that don't inherit from each other. In the separate
> scheme, each chain can use it's own discriminator column, but you
> cannot combine two class hierarchies that each use a different
> discriminator column. 

hum, so something is wrong here, I recreate a simple test case and all
is working properly, using the exactly model suggested on my previous
message. I will debug my application to find where is the error, but for
now, I don't know if this should work, but is working.

here is the test case and results:

----code-----8<------code------

from sqlalchemy import create_engine, MetaData, Table, Column, types,
ForeignKey
from sqlalchemy.orm import mapper, relation, backref, create_session
from sqlalchemy import String, Unicode, Integer, DateTime, Numeric,
Boolean, UnicodeText


db = create_engine('sqlite:///:memory:')

metadata = MetaData()
metadata = MetaData(db)
metadata.bind = db
session = create_session(bind=db)


resource_table = Table('resource', metadata,
    Column('id',Integer, primary_key=True),
    Column('name', String(30)),
    Column('poly', String(31), nullable=True)
)

person_table = Table('person', metadata,
    Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
    Column('poly', String(31), nullable=False)
)

material_table = Table('material', metadata,
    Column('id',Integer, ForeignKey('resource.id'), primary_key=True,),
)

employee_table = Table('employee', metadata,
    Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

technical_table = Table('technical', metadata,
    Column('id',Integer, ForeignKey('person.id'), primary_key=True),
)

class Resource(object):
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return "Resource < id=%d ,name=%s >" % (self.id,self.name)

class Person(Resource):
    def __repr__(self):
        return "Person < id=%d ,name=%s >" % (self.id,self.name)

class Material(Resource):
    def __repr__(self):
        return "Material < id=%d ,name=%s >" % (self.id,self.name)

class Employee(Person):
    def __repr__(self):
        return "Employee < id=%d ,name=%s >" % (self.id,self.name)

class Technical(Person):
    def __repr__(self):
        return "Technical < id=%d ,name=%s >" % (self.id,self.name)

mapper(Resource, resource_table,
    polymorphic_on=resource_table.c.poly,
    polymorphic_identity='resource'
    )

mapper(Person, person_table, 
    polymorphic_on=person_table.c.poly,
    inherits=Resource, polymorphic_identity='person'
    )

mapper(Material, material_table, 
    inherits=Resource, polymorphic_identity='material'
    )

mapper(Employee, employee_table,
    inherits=Person, polymorphic_identity='employee',
    )

mapper(Technical, technical_table,
    inherits=Person, polymorphic_identity='technical',
    )

metadata.create_all(bind=db)


r = Resource('resource name')
p = Person('person name')
m = Material('material name')
e = Employee('employee name')
t = Technical('technical name')

session.save(r)
session.save(p)
session.save(m)
session.save(e)
session.save(t)

session.flush()
print "############ LIST FROM RESOURCES #################"

for o in session.query(Resource).all():
    print o

print "############ LIST FROM PERSONS #################"

for o in session.query(Person).all():
    print o


----code-----8<------code------

The results:

############ LIST FROM RESOURCES #################
Resource < id=1 ,name=resource name >
Person < id=2 ,name=person name >
Material < id=3 ,name=material name >
Employee < id=4 ,name=employee name >
Technical < id=5 ,name=technical name >
############ LIST FROM PERSONS #################
Person < id=2 ,name=person name >
Employee < id=4 ,name=employee name >
Technical < id=5 ,name=technical name >



I think it is working properly.

but I will try to remove the type columns

Thank's for help
-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to