robert -

SQLite doenst like the LIMIT without an OFFSET that occurs when you do get_by. I added an OFFSET 0 to the sqlite module when theres a LIMIT, now your example works.

- mike


On Mar 5, 2006, at 5:13 PM, Robert Leftwich wrote:

Perhaps I'm misunderstanding what select_by and get_by should do, but I thought that a select_by and a get_by should return identical results to a get and select with the same parameters, but they are not. The attached example below illustrates this, the assertions fail unless the objectstore.clear() between each retrieval is removed. Note that I was writing this test to debug an even trickier problem to do with backrefs but I think they are related and this is simpler, so it's a good starting point.

Robert

PS This is with r1097
from sqlalchemy import *

db = engine.create_engine('sqlite', {'filename':':memory:'}, echo=True)

users_table = Table('users', db,
Column('user_id', Integer, Sequence('user_id_seq', optional=True), primary_key = True),
    Column('user_name', String(40)),

)

addresses_table = Table('addresses', db,
Column('address_id', Integer, Sequence ('address_id_seq', optional=True), primary_key = True), Column('user_id', Integer, ForeignKey ("users.user_id")),
                        Column('address', String(40)),
                        )

phones_table = Table('phone_numbers', db,
Column('phone_id', Integer, Sequence ('phone_id_seq', optional=True), primary_key = True), Column('address_id', Integer, ForeignKey ('addresses.address_id')),
                        Column('type', String(20)),
                        Column('number', String(10)),
                        )

users_table.create()
addresses_table.create()
phones_table.create()

class User(object):
    def __init__(self):
        self.user_id = None
    def __repr__(self):
return "User:" + repr(getattr(self, 'user_id', None)) + " " + repr(getattr(self, 'user_name', None)) + " " + str([repr(addr) for addr in self.addresses])

class Address(object):
    def __repr__(self):
return "Address: " + repr(getattr(self, 'address_id', None)) + " " + repr(getattr(self, 'user_id', None)) + " " + repr (self.address) + str([repr(ph) for ph in self.phones])

class Phone(object):
    def __repr__(self):
return "Phone: " + repr(getattr(self, 'phone_id', None)) + " " + repr(getattr(self, 'address_id', None)) + " " + repr (self.type) + " " + repr(self.number)

Phone.mapper = mapper(Phone, phones_table, is_primary=True)

Address.mapper = mapper(Address, addresses_table, properties={
    'phones': relation(Phone.mapper, lazy=False)
    })

User.mapper = mapper(User, users_table, properties={
    'addresses' : relation(Address.mapper, lazy=False),
    })

objectstore.clear()
u1 = User()
u1.user_name = 'user 1'

a1 = Address()
a1.address = 'a1 address'

p1 = Phone()
p1.type = 'home'
p1.number = '1111'

a1.phones.append(p1)

p2 = Phone()
p2.type = 'work'
p2.number = '22222'
a1.phones.append(p2)

u1.addresses.append(a1)

a2 = Address()
a2.address = 'a2 address'

p3 = Phone()
p3.type = 'home'
p3.number = '3333'
a2.phones.append(p3)

p4 = Phone()
p4.type = 'work'
p4.number = '44444'
a2.phones.append(p4)

u1.addresses.append(a2)

objectstore.commit()
objectstore.clear()

a = User.mapper.get(1)
print a

objectstore.clear()

b = User.mapper.select_by(user_id=1)[0]
print b

assert repr(a) == repr(b)

objectstore.clear()

c = User.mapper.select_by(user_name='user 1')[0]
print c

assert repr(a) == repr(b) == repr(c)

objectstore.clear()

d = User.mapper.get_by(user_name='user 1')
print d

assert repr(a) == repr(b) == repr(c) == repr(d)



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to