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

Open
#720 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
database

Research direction

Start with the runnable Python reproduction in the issue and inspect pydal/objects.py, especially Row.getattr and getitem, where the traceback points. Confirm the behavior with and without with_alias(); done means non-aliased fields remain accessible alongside aliased fields in the resulting row.

Written by the indexing model from the issue text.

Description

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

Dominant language
Python
Stars
540
Forks
151
Avg merge
12d 14h
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from web2py/pydal

All issues in web2py/pydal

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.