Hi, 

I have a class which represents a table with polymorphic enabled. When I 
directly access to this table the polymorphic mechanism adds the needed 
condition and everything works perfectly. However when this table is joined to 
a request the condition is missing…

I attached a quick sample to this email.
My output:

2014-10-10 00:18:44,582 INFO sqlalchemy.engine.base.Engine SELECT 
"myTable"."otherId" AS "myTable_otherId" 
FROM "myTable" 
WHERE "myTable".id = ? AND "myTable".flag IN (?)
2014-10-10 00:18:44,582 INFO sqlalchemy.engine.base.Engine (1, 1)
A contains 2
A contains 3
2014-10-10 00:18:44,583 INFO sqlalchemy.engine.base.Engine SELECT 
"otherTable".id AS "otherTable_id", "myTable"."otherId" AS "myTable_otherId" 
FROM "otherTable" JOIN "myTable" ON "myTable".id = "otherTable".id
2014-10-10 00:18:44,583 INFO sqlalchemy.engine.base.Engine ()
Now A contains 2
Now A contains 3
Now A contains 4
Now A contains 5


The last lines are not part of the A class due to the polymorphic constraint… 
However they are present since the polymorphic condition is missing.

Maybe I missed something?

Thank you for your help.
Sincerely

Aurélien
#!/usr/bin/python2.7
from sqlalchemy import Column, Integer, Boolean, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()


class _motherClass(Base):
    __tablename__ = 'myTable'
    id = Column(Integer(unsigned=True), primary_key=True)
    otherId = Column(Integer(unsigned=True), primary_key=True)
    flag = Column(Boolean, primary_key=True)
    __mapper_args__ = {
        'polymorphic_on'        : flag,
    }
    def __init__(self, id, otherId) :
        self.id = id
        self.otherId = otherId
 
class A(_motherClass) :
    __mapper_args__ = {
        'polymorphic_identity'  : True,
    }

class B(_motherClass) :
    __mapper_args__ = {
        'polymorphic_identity'  : False,
    }

class OtherTable(Base) :
    __tablename__ = 'otherTable'
    id = Column(Integer(unsigned=True), primary_key=True)


engine = create_engine('sqlite:///test.sqlite3', echo=True)
session = sessionmaker(bind=engine)()
Base.metadata.create_all(engine)

session.add(A(1, 2))
session.add(A(1, 3))
session.add(B(1, 4))
session.add(B(1, 5))
session.add(OtherTable(id=1))
session.commit()

for (otherId,) in session.query(A.otherId).filter(A.id == 1) :
	print('A contains %s' % otherId)

for (id, otherId,) in session.query(OtherTable.id, A.otherId).join(A, A.id == OtherTable.id) :
	print('Now A contains %s' % otherId)
-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to