Hello all,

I am getting the error bellow with polymorphic inheritance turned off.

sqlalchemy.exceptions.FlushError: Attempting to flush an item of type
<class '__main__.Representative'> on collection 'Goal.person (Person)',
which is handled by mapper 'Mapper|Person|person' and does not load
items of that type.  Did you mean to use a polymorphic mapper for this
relationship ?  Set 'enable_typechecks=False' on the relation() to
disable this exception.  Mismatched typeloading may cause bi-directional
relationships (backrefs) to not function properly.


here a test case by illustrating the same behavior, note that the test
case use all options I am using in my project:

it's very strange, because if I turn polymorphic = True, seems to work
but message says that is the inverse (or I am wrong understanding).

follow the test case using Elixir and "the same aproach" using
sqlalchemy, it this test SQLAlchemy doesn't matter if I use polymorphic
or not, but sometime ago, I created a similar structure and got the same
error using polymorphic loads.

Any suggestion?


test_elixir.py
---------8<-----------------------------------------------------------
from elixir import Entity, Field, session, OneToMany, ManyToOne,
options_defaults
from elixir import setup_all, create_all, UnicodeText, metadata,
Integer, options
import re

metadata.bind = "sqlite:///test.db"

def camel_to_underscore(entity):
    return re.sub(r'(.+?)([A-Z])+?', r'\1_\2', entity.__name__).lower()

options_defaults['autosetup'] = False
options_defaults['shortnames'] = True
options_defaults['inheritance'] = 'multi'
options_defaults['tablename'] = camel_to_underscore
options_defaults['polymorphic'] = False
options.MULTIINHERITANCECOL_NAMEFORMAT = "%(key)s"

class Goal(Entity):
    person = ManyToOne('Person')
    value = Field(Integer)

class Person(Entity):
    name = Field(UnicodeText)
    goals = OneToMany('Goal')
    def __repr__(self):
        return self.name

class Representative(Person):
    pass


setup_all()
create_all()

r = Representative(name=u"Alexandre")
g = Goal(value=100)
r.goals.append(g)

session.flush()
session.clear()

k = Representative.get(1)
print k.goals
---------8<-----------------------------------------------------------

test_sa.py
---------8<-----------------------------------------------------------
from sqlalchemy import Table, Column, ForeignKey, UnicodeText, Unicode,
Integer
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import mapper, relation, sessionmaker

engine = create_engine('sqlite:///teste_sa.db')
metadata = MetaData(engine)
DatabaseSession = sessionmaker(bind=engine, autoflush=True,
transactional=True)
session = DatabaseSession()


goal_table = Table('goal', metadata,
    Column('id', Integer, primary_key=True),
    Column('person_id', Integer, ForeignKey('person.id')),
    Column('value', Integer),
)

person_table = Table('person', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', UnicodeText),
    Column('row_type', Unicode(40)),
)

representative_table = Table('representative', metadata,
    Column('id', Integer, ForeignKey('person.id', ondelete='cascade'),
primary_key=True),
)

class Goal(object):
    def __init__(self, value=None):
        self.value=value

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

class Representative(Person):
    pass

mapper(Goal, goal_table,
    properties=dict(
        person=relation(Person, uselist=False)
    )
)
mapper(Person, person_table,
    #polymorphic_on=person_table.c.row_type,
    #polymorphic_identity=u'person',
    properties=dict(
        goals=relation(Goal)
    )
)
mapper(Representative, representative_table,
    inherits=Person,
    #polymorphic_identity=u'representative',
)

metadata.create_all()

r = Representative(name=u"Alexandre")
g = Goal(value=100)
r.goals.append(g)
session.save(r)
session.commit()
session.flush()
session.clear()

k = session.query(Representative).get(1)
print k.goals
---------8<-----------------------------------------------------------
-- 
Alexandre da Silva
Analista de Sistemas - Bacharel em Sistemas de Informação (2003-2007)
-- 
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 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to