Would None (ie. SQL NULL) be a valid polymorphic identity? > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Michael Bayer > Sent: 30 January 2009 17:06 > To: [email protected] > Subject: [sqlalchemy] Re: polymorphic_identity not allowed to > be zero for base class? > > its likely a bug. there are some boolean tests for > "polymorphic_identity" in mapper.py which should be changed > to "is None". > > > On Jan 30, 2009, at 11:58 AM, Gunnlaugur Thor Briem wrote: > > > Hi, > > [trying to send this again, seems like previous copy > got lost in some moderation queue] > > Messing with single-table inheritance in a declarative > model, with a non-abstract base class, I find that querying > fails if polymorphic_identity is 0 (zero). Example: > > ################ code begins ################ > from sqlalchemy import Column, Integer, create_engine > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy.orm import sessionmaker > > Base = declarative_base() > class Course(Base): > __tablename__ = 'course' > id = Column(Integer, primary_key=True) > course_type = Column(Integer) > __mapper_args__ = {'polymorphic_on':course_type, > 'polymorphic_identity':0} > > class MultiYearCourse(Course): > __mapper_args__ = {'polymorphic_identity':1} > > engine = create_engine('sqlite:///: > memory:') > Base.metadata.create_all(bind=engine) > session = sessionmaker(bind=engine)() > > myc = MultiYearCourse() > myc.name <http://myc.name/> = u"Computer Graphics" > c = Course() > c.name <http://c.name/> = u"Sociology" > session.add(c) > session.add(myc) > session.commit() > > print "MYC: %s" % myc > print "C: %s" % c > query = session.query(Course) > print "Query: %s" % query > print "Results: %s" % query.all() > ################ code ends ################ > > That last line fails with an AssertionError: > > ################ output begins ################ > MYC: <__main__.MultiYearCourse object at 0xcf7d30> > C: <__main__.Course object at 0xcf7d70> > Query: SELECT course.id <http://course.id/> AS > course_id, course.course_type AS course_course_type > FROM course > Traceback (most recent call last): > File > "/Users/gthb/Documents/workspace/test/src/sqlalchemytest.py", > line 31, in <module> > print "Results: %s" % query.all() > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/orm/query.py", line 1186, in all > return list(self) > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/orm/query.py", line 1341, in instances > rows = [process[0](context, row) for row in fetch] > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/orm/query.py", line 1942, in main > return _instance(row, None) > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/orm/mapper.py", line 1557, in _instance > _instance = polymorphic_instances[discriminator] > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/util.py", line 71, in __missing__ > self[key] = val = self.creator(key) > File > "/Users/gthb/Library/Python/2.5/site-packages/SQLAlchemy-0.5.2 > -py2.5.egg/sqlalchemy/orm/mapper.py", line 1695, in > configure_subclass_mapper > raise AssertionError("No such polymorphic_identity > %r is defined" % discriminator) > AssertionError: No such polymorphic_identity 0 is defined > ################ output ends ################ > > But if I exchange the polymorphic identities, so the > base class gets the 1 and the subclass gets the 0, then it > runs just fine! > > It seems to me that this can't be intentional - don't > see a reason for it, and the docs do not mention any > particular restrictions on values of polymorphic_identity. > > Regards, > > - Gulli > > > > > > > > > >
--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" 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/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
