Hey all,

More fun with inheritance and mappers.  In the following situation
where the polymorphic type is a Boolean, the mapper for the object
that matches on False incorrectly returns the parent object.

Cheers
Dave

------------------------------
-- testapi.py ----------------
------------------------------

from sqlalchemy import *
from sqlalchemy.orm import *

session = scoped_session(
    sessionmaker(autoflush=False, transactional=True)
)
mapper = session.mapper
metadata = MetaData()
 
ANIMAL_TYPE_DOG = True
ANIMAL_TYPE_CAT = False

VET_TYPE_CITY = 1
VET_TYPE_COUNTRY = 2

animalTable = Table(
    'animal',
    metadata,
    Column('id', Integer, primary_key=True),
    Column('type', Boolean, nullable=False),
    Column('name', String(100), nullable=False),
    Column('vet_id', Integer, ForeignKey('vet.id')),
)

vetTable = Table(
    'vet',
    metadata,
    Column('id', Integer, primary_key=True),
    Column('address', String(100), nullable=False),
    Column('kennel', Boolean, nullable=False),
)

class _Animal(object):
    pass
class Cat(_Animal):
    pass
class Dog(_Animal):
    pass
class Vet(object):
    pass

vetMapper = mapper(
    Vet,
    vetTable,
    properties = {
        "animals": relation(
            _Animal,
            backref=backref("vet", uselist=False),
            cascade="all, delete-orphan"
        ),
    }
)

animalMapper = mapper(
    _Animal,
    animalTable,
    polymorphic_on=animalTable.c.type,
)
mapper(
    Dog,
    inherits=animalMapper,
    polymorphic_identity=ANIMAL_TYPE_DOG,
)
mapper(
    Cat,
    inherits=animalMapper,
    polymorphic_identity=ANIMAL_TYPE_CAT,
)

def connect(uri):
    engine = create_engine(uri, strategy="threadlocal")
    metadata.bind = engine
    return engine

def create():
    metadata.create_all()

def drop():
    metadata.drop_all()

------------------------------
-- test.py ----------------
------------------------------

import sys
import testapi as db

DB = "sqlite:///memory"
db.connect(DB)
db.create()

v = db.Vet()
v.address = "12 Foo St"
v.kennel = True

c1 = db.Cat()
c1.name = "muffin"

c2 = db.Cat()
c2.name = "bagel"

d1 = db.Dog()
d1.name = "rex"

d2 = db.Dog()
d2.name = "bill"

db.session.flush()
db.session.clear()

for d in db.Query(db.Dog).all():
    v.animals.append(d)
for c in db.Query(db.Cat).all():
    v.animals.append(c)

db.session.flush()
db.session.clear()

v = db.Query(db.Vet).first()
for a in v.animals:
    print a



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