so then you start with a simple example and then slowly modify your real program one line at a time until it looks like your simple example, each time testing to see what causes the problem.
here's all your test cases
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
Base = declarative_base()
class Company(Base):
__tablename__ = "company"
id = Column(Integer, primary_key=True)
employees = relationship("Person", back_populates="company")
class Person(Base):
__tablename__ = "people"
id = Column(Integer, primary_key=True)
discriminator = Column("type", String(50))
__mapper_args__ = {
"polymorphic_on": discriminator,
"polymorphic_identity": "person",
}
company = relationship(
"Company", lazy="joined", back_populates="employees"
)
class Engineer(Person):
__tablename__ = 'engineer'
id = Column(Integer, ForeignKey('
people.id'), primary_key=True)
primary_language = Column(String(50))
__mapper_args__ = {
"polymorphic_identity": "engineer",
}
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add(Company(employees=[Engineer(primary_language="java")]))
s.commit()
s.close()
e1 = s.query(Engineer).get(1)
# close so we assert no sql in the next step
s.close()
# no query
assert e1.company is not None
engineer = s.query(Engineer).get(1)
s.commit()
person = s.query(Person).get(1) # emits 1 query
# same object
assert engineer is person
# s.close() # you can do this if you are on 1.4. in 1.3, the .company attr is not loaded
# emits a query in 1.3 because the attribute was expired by the commit
person.company
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.