`object has no attribute` error on join
See original GitHub issueSuppose I have a many-to-one relationship between products
and departments
, where both tables have a joinrow_id
column. Suppose that we only have one product and its name
is "chair"
. The chair is in the only department, whose name is “all products”, and whose “priority” is null.
I’m seeing inconsistent behavior between these two queries:
builder = Products.select(Products.name,
Departments.name)\
.join(Departments, on=(Departments.joinrow == Products.joinrow).alias('department'))\
.switch(Products)\
.where(Products.id == 1)
builder = Products.select(Products.name,
Departments.priority)\
.join(Departments, on=(Departments.joinrow == Products.joinrow).alias('department'))\
.switch(Products)\
.where(Products.id == 1)
In the first case, seemingly because the name
field is truthy, I am able to type: [r for r in builder][0].department.name
But in the second case, if I try [r for r in builder][0].department.priority
, the library raises an error:
Products has no attribute 'department'
.
This is easily remedied. I just have to make sure that, in my select, I include a column from departments
whose value is always truthy.
I just wanted to bring this behavior to your attention in case you weren’t aware.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (5 by maintainers)
Top GitHub Comments
For your example, you should be able to workaround the “NULL” priority by also selecting the Department’s primary key, e.g.
I’ve created a self-contained example here:
https://gist.github.com/skatenerd/45b65a361384869e39ee20069d8ea7d4
On my machine, I’m able to make the first print statement in
go()
, but the second crashes withAttributeError: 'Products' object has no attribute 'department'