- changed title to eager loading broken for manytoone->onetomany
eager loading broken for manytoone->onetomany
Issue #96
resolved
(original reporter: rtl) I have narrowed the test case for this problem down to the following:
from sqlalchemy import *
import datetime
db = create_engine('sqlite', {'filename':':memory:'}, echo='debug')
companies_table = Table('companies', db,
Column('company_id', Integer, Sequence('company_id_seq', optional=True), primary_key = True),
Column('company_name', String(40)),
)
addresses_table = Table('addresses', db,
Column('address_id', Integer, Sequence('address_id_seq', optional=True), primary_key = True),
Column('company_id', Integer, ForeignKey("companies.company_id")),
Column('address', String(40)),
)
invoice_table = Table('invoices', db,
Column('invoice_id', Integer, Sequence('invoice_id_seq', optional=True), primary_key = True),
Column('company_id', Integer, ForeignKey("companies.company_id")),
Column('date', DateTime),
)
companies_table.create()
addresses_table.create()
invoice_table.create()
class Company(object):
def __init__(self):
self.company_id = None
def __repr__(self):
return "Company:" + repr(getattr(self, 'company_id', None)) + " " + repr(getattr(self, 'company_name', None)) + " " + str([for addr in self.addresses](repr(addr)))
class Address(object):
def __repr__(self):
return "Address: " + repr(getattr(self, 'address_id', None)) + " " + repr(getattr(self, 'company_id', None)) + " " + repr(self.address)
class Invoice(object):
def __init__(self):
self.invoice_id = None
def __repr__(self):
return "Invoice:" + repr(getattr(self, 'invoice_id', None)) + " " + repr(getattr(self, 'date', None)) + " " + repr(self.company)
Address.mapper = mapper(Address, addresses_table, properties={
})
Company.mapper = mapper(Company, companies_table, properties={
'addresses' : relation(Address.mapper, lazy=False),
})
Invoice.mapper = mapper(Invoice, invoice_table, properties={
'company': relation(Company.mapper, lazy=False, )
})
c1 = Company()
c1.company_name = 'company 1'
a1 = Address()
a1.address = 'a1 address'
c1.addresses.append(a1)
a2 = Address()
a2.address = 'a2 address'
c1.addresses.append(a2)
i1 = Invoice()
i1.date = datetime.datetime.now()
i1.company = c1
objectstore.commit()
objectstore.clear()
c = Company.mapper.get(1)
objectstore.clear()
i = Invoice.mapper.get(1)
print c
print i.company
assert repr(c) == repr(i.company)
Comments (2)
-
repo owner -
repo owner - changed status to resolved
checked in this test and the original one with backrefs etc. as a unittest. fixes are changeset:1104.
- Log in to comment
retrieving the Company by itself will eager load its addresses properly. retreiving the Invoice, with its many-to-one Company, will halt loading of the single Company object after its loaded, and not load the rest of the addresses.
a one line fix + a new set of unit tests forthcoming.