Skip to content

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

@laundmo

Description

@laundmo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions