question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `Case().alias` with join

See original GitHub issue

Let’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:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Aug 12, 2022

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.

0reactions
coleifercommented, Aug 12, 2022

Let me think about it for a bit this morning.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found