Using `Case().alias` with join
See original GitHub issueLet’s say I have the following models:
class AttachedFile(Model):
article = ForeignKeyField(Article)
file_name = StringField() # this is public
file_url = StringField() # but this field requires users to be subscribed
class Article(Model):
# ...
attached_file = ForeignKeyField(AttachedFile)
# ...
class User(Model):
# ...
class Subscription(Model):
# ...
user = ForeignKeyField(User)
# ...
So what I’m trying to do is to “hide” the article.attached_file.file_url
field if the user doesn’t have a subscription. So:
user_id = 3 # dummy id
sub_cte = (Subscription.select()
.where(Subscription.user == user_id).cte('sub_cte'))
article_fields = [
AttachedFile.id,
AttachedFile.file_name,
Case(fn.EXISTS(sub_cte.select()), [(False, None)], AttachedFile.file_url).alias("file_url") # <- HERE IS THE PROBLEM
]
target_article = (Article.select(*article_fields)
.join_from(Article, User)
.join_from(Article, AttachedFile)
.with_cte(sub_cte)
.get_or_none())
if target_article is not None:
print(target_article.attached_file.file_url)
Now the file_id
belongs to target_article
not to the target_article.attached_file
, and the structure of target_article
is not what I want.
I even tried this:
Case(...).alias(AttachedFile.file_url)
# or
Case(...).alias('"t1"."file_url"')
But it’s raising an error.
What I CANNOT do:
- not using cte’s
- using subqueries
- manually setting
target_article.attached_file.file_url
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How to Use Aliases with SQL JOINs | LearnSQL.com
SQL aliases are custom names that you can give to the columns and tables you include in your queries. Aliases are very useful, ......
Read more >JOIN statement on an alias - SQL Server - Stack Overflow
1st way: Use the Expression in the SELECT and the ON clause. Thus: SELECT CASE WHEN T1.[No1] ; 2nd way: Use a Subselect:...
Read more >Joins and aliases
To combine data from two tables we use the SQL JOIN command, which comes after the FROM command. The JOIN command on its...
Read more >Joins and aliases - SQL - Library Carpentry
The SQL JOIN clause allows us to combine columns from one or more tables in a database by using values common to each....
Read more >SQL Alias - Dofactory
Table aliases simplify writing JOIN and ORDER BY clauses. The C alias in C.Id helps identify the Customer Id versus the Order Id....
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
So the benefit of using
.bind_to()
is that you can specify the destination model in a multi-model query regardless of whether the alias corresponds to a field. For example, from the tests:https://github.com/coleifer/peewee/commit/eeff376be5af089e7d3849284a1c1d71b5df215f#diff-969c0712020e8472d8d78ae32190692cc1e8a3e16dc17f6977c8f87a1328adb7R4896-R4904
In the above, we alias a Case expression to a name that does not correspond to a field, and also specify that it should be applied on the related
User
instance.If
Alias()
is extended to support a field/column as the destination, then we lose this functionality.I think I’m going to leave it as-is for now, even though it is more verbose in your particular examples.
Let me think about it for a bit this morning.