[Django]-Django Queryset: column reference is ambiguous with extra queryset

8👍

This is nothing to do with Django. You’re inserting raw SQL (the extract clause) but failing to qualify the table that updated_at is coming from within it. Assuming the Article model is in an app called “myapp” it would be something like this:

select={'hour': 'extract(hour from myapp_article.updated_at)'})

7👍

If you’d come here through a web search: I started having the error column reference "created" is ambiguous when I added prefetch_related and select_related statements to my filter.

While originally my query .extra(where=['EXTRACT(hour from created) >19 OR EXTRACT(hour from created) <6']) worked, it stopped working when select_related/prefetch) was added.

The solution was to add myappname_mymodelname. before the field name.

👤SaeX

Leave a comment