Error when using "alias" as a field name in m2m tables
See original GitHub issueWell, it’s me again. 😃 I met something really weird. It seems like we can’t use field name alias
in m2m tables i.e. prefetch_related won’t work with it.
Is it reserved for something?
I have a model structure like this:
class ModelOne(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
many_to_many = fields.ManyToManyField('models.ModelTwo')
class ModelTwo(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
alias = fields.TextField()
And after using prefetch related (code snippet below):
async for item in ModelOne.all().prefetch_related('many_to_many'):
print(item)
we got errors like these:
Traceback (most recent call last):
File "/opt/project/src/server.py", line 54, in <module>
asyncio.run(main())
File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 579, in run_until_complete
return future.result()
File "/opt/project/src/server.py", line 47, in main
async for item in ModelOne.all().prefetch_related('many_to_many'):
File "/usr/local/lib/python3.7/site-packages/tortoise/queryset.py", line 512, in __aiter__
for val in await self:
File "/usr/local/lib/python3.7/site-packages/tortoise/queryset.py", line 521, in _execute
).execute_select(self.query, custom_fields=list(self._annotations.keys()))
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 90, in execute_select
await self._execute_prefetch_queries(instance_list)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 327, in _execute_prefetch_queries
await self._do_prefetch(instance_list, field, related_query)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 320, in _do_prefetch
return await self._prefetch_m2m_relation(instance_id_list, field, related_query)
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 232, in _prefetch_m2m_relation
*[getattr(related_query_table, field).as_(field) for field in related_query.fields],
File "/usr/local/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 232, in <listcomp>
*[getattr(related_query_table, field).as_(field) for field in related_query.fields],
AttributeError: 'NoneType' object has no attribute 'as_'
tortoise-orm==0.14.0, asyncpg==0.19.0
PS: If you rename alias->alias2 this code will work.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Error using Table Alias - Stack Overflow
Try: DELETE a FROM tablename a WHERE a.fieldname > 10. (Of course, aliases are hardly useful in single table statements.) ...
Read more >Create a many-to-many relationship
Many-to-many relationships allow a list to point to a list of entries, rather than to single field.
Read more >Column name alias error. — DataTables forums
I am using datatables v1.10.2 server side code and am joining multiple tables. All is well until I give a column name an...
Read more >Relationships | Directus Docs
When we use classic data model terms, such as data table, column, row, ... recipes - id - name - ingredients (An M2M...
Read more >Untitled
By inferring the relationship between tables, join conditions are added for ... Get the root table name with its relationships according to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Works as expected. Amazing work. Thank you. 👌
I just release 0.14.2, please test that it resolved the issue for you 😃