Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db(...).select('id').first().id now gives an AttributeError #246

Closed
kjkuan opened this issue Jul 3, 2015 · 6 comments
Closed

db(...).select('id').first().id now gives an AttributeError #246

kjkuan opened this issue Jul 3, 2015 · 6 comments

Comments

@kjkuan
Copy link
Contributor

kjkuan commented Jul 3, 2015

This used to work... . Now, the id attribute is placed inside an _extra dict.
Example:

>>> db(db.auth_user.registration_id==username).select('id')[0]
<Row {'_extra': {'id': 2}}>

This actually happens to any fields you select() not just id.

@gi0baro
Copy link
Member

gi0baro commented Jul 3, 2015

Ok, we finally had a real use-case for #229
I'm gonna re-open the other issue since this is a consequence.

@ilvalle
Copy link
Contributor

ilvalle commented Jul 6, 2015

@kjkuan the output is correct. On both master and v0.12.25 you get the same output

db=DAL('sqlite:memory')
db.define_table('tt', Field('vv', 'integer'))
db.tt.insert(vv=1)
print db(db.tt).select(db.tt.vv).first()
print db(db.tt).select('vv').first()
<Row {'vv': 1L}>
<Row {'_extra': {'vv': 1}}>

However it's not clear to me why that happen, you should get in both situations the first output.
Is it a design choice? If yes, I'll update Row to support this behavior else I'll try to fix it so that you get the same output on both cases. @mdipierro what do you think ?

@mdipierro
Copy link
Contributor

No this is correct. The problem is that something else is broken.
x = db(db.tt).select('vv').first()
print x['vv'] # (1)
print x.vv # (2)
(1) worked and still works but (2) used to work and no longer works. I think the change that cause this is only calling for trouble and I do not see what value it added.

@ilvalle
Copy link
Contributor

ilvalle commented Jul 6, 2015

so adding __getattr__ = __getitem__ to Row should fix it

@gi0baro
Copy link
Member

gi0baro commented Jul 6, 2015

@ilvalle nope. See #241

@mdipierro
Copy link
Contributor

I run this tests with the latest trunk:
db = DAL('sqlite:memory')
db.define_table('x',Field('y','integer'))
db.x.insert(y=1)
row = db(db.x).select(db.x.id,db.x.y).first()
assert row.y==1
assert row.id==1
row = db(db.x).select(db.x.id,db.x.y,'y',db.x.y.sum(),groupby=db.x.id).first()
print row
assert row.x.id==1
assert row.x.y==1
assert row['y']==1
assert row['x.y']==1
assert row('y')==1
assert row('x.y')==1
assert row[db.x.y.sum()]==1
assert row(db.x.y.sum())==1
assert row._extra['y']==1
assert row._extra[db.x.y.sum()]==1
row = db(db.x).select('y').first()
print row

assert row.y==1

assert row['y']==1
assert row('y')==1

except for the commented one they all pass. And honestly I cannot say the commented one should pass. In fact probably it is correct that id does not. So unless we introduced a slow down in the code I think everything is fine here.

gi0baro added a commit that referenced this issue Jul 10, 2015
added __getattr__ in Row, fix #229, close #246
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants