21 Replies
The problem is
update <table as t>
I don't think drizzle currently have a way to do table alias with as
You can, there’s an
alias()
functionOnly in the subquery you could use drizzle syntax
Yeah, I saw some .as() queries, but not with update. Still new to Drizzle and it was easy enough to do a raw SQL query, so I didn't dig into it too much.
I just tested the alias function, it doesn't work properly inside the update
Ah, that should help me write my own, thansk
I’m not sure that the alias should be necessary here, fwiw
this produces: 'update
us
set created_at
= select max(created_at) from posts
po
where po
.author_id
= us
.id
'Will give this a shot shortly
Don't use my example, doesn't work
I think the canonical way to do this would be
UPDATE FROM
, right? Which drizzle doesn’t support yet https://github.com/drizzle-team/drizzle-orm/issues/208What i'm trying to say is that the alias doesn't get aliased in the update for some reason
I've never used update from
But updating with a subquery from another table is what he's after
Querying with SQL-like syntax [CRUD] – DrizzleORM
Drizzle ORM | %s
Yeah there’s missing handling for that, just checked
So, if I were to file a feature request GH issue for this, what's a good title for it? '[FEATURE]: Support aliases for update queries' ?
This is what I was kinda able to do
Which is not a great improvement over what you had
But it's working
Why is the alias needed at all?
I did try it without the alias
Honestly, at this point I changed my mind about updating them all at once, and want to target each feed individually anyway, due to the way each feed can time out and run errors
Honestly, I just can't wrap my head around the Drizzle syntax
FYI, this seems like it should work:
As far as drizzle syntax goes, it almost 1:1 maps to raw SQL. In this case, the notable gotchas would be:
- You can't currently use an alias for the table you're updating (bug/unimplemented) - but you also don't need it, it just adds extra work since in drizzle, you want to be using the actual table variables anyways, and only alias when you need to differentiate
- You have to wrap the subquery in a
sql
template string, as set
doesn't currently support a subquery directly
- You have to name the field in the select (this winds up being how it would map back to a JS object, even though that's unused in this case)
- There isn't a builtin for MAX
, so again you need a sql template stringAlmost. Just needed a tweak:
.where(eq(feedItems.feedId, feeds.id))
Thanks for writing this up, especially with the detailed notes! I've been using Prisma for so long, my SQL skills have atrophied 🙂
Naming the field max
, but still having lastPublishedAt:
work like that without a .max
definitely looks counterintuitive for a novice to the API like it.
I also hadn't thought about putting a whole db...
chain into a sql
call like that, especially without it ending in a .toSQL()
.
But it works 🙂Yeah, they’re workarounds for current limitations, so hopefully will become more intuitive over time