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

with_alias in select causes non-aliased fields to become nested under their tablename #720

Open
laundmo opened this issue Nov 18, 2024 · 0 comments

Comments

@laundmo
Copy link

laundmo commented Nov 18, 2024

Using with_alias() in .select causes non-aliasd fields to be inaccessible on the resulting row:

Without alias, this works:

rows = db().select(db.mytable.field1, db.mytable.field2)
row = next(iter(rows))
print(row) # <Row {'field1': 'A', 'field2': 'B'}>
row.field1

With alias, this fails with AttributeError during handling of KeyError:

rows = db().select(db.mytable.field1, db.mytable.field2.with_alias("field_two"))
row = next(iter(rows))
print(row) # <Row {'mytable': {'field1': 'A'}, 'field_two': 'B', '_extra': {'"mytable"."field2" AS field_two': 'B'}}>
row.field1

Error caused:

Traceback (most recent call last):
  File ".venv\lib\site-packages\pydal\objects.py", line 167, in __getattr__
    return self.__getitem__(k)
  File ".venv\lib\site-packages\pydal\objects.py", line 147, in __getitem__
    raise KeyError(key)
KeyError: 'field1'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    row.field1
  File ".venv\lib\site-packages\pydal\objects.py", line 169, in __getattr__
    raise AttributeError
AttributeError

version:

>>> pydal.__version__
'20241111.2'
Full runnable reproduction

from pydal import DAL, Field

db = DAL("sqlite:memory")
db.define_table("mytable", Field("field1"), Field("field2"))

db.mytable.insert(field1="A", field2="B")

rows = db().select(db.mytable.field1, db.mytable.field2)
row = next(iter(rows))
print(row)
row.field1

rows = db().select(db.mytable.field1, db.mytable.field2.with_alias("field_two"))
row = next(iter(rows))
print(row)
row.field1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant