DT
Drizzle TeamGuilherme Rosado
[HELP]: I'm trying to re-create a CTE-insert in drizzle, having some difficulties
How does the query above translate to drizzle?
I think more examples that translates to the above would be awesome in the docs, I've been fighting a bit with the API. I know from the start how to do it on sql, but sometimes it gets really hard translating to drizzle.
I feel this could be solved with more examples, maybe?
rphlmr ā”ā¢246d ago
We spoke about that few days ago here:https://discord.com/channels/1043890932593987624/1145679831841919006 (scroll to the end)
Hope it helps
Guilherme Rosadoā¢246d ago
Well, looking at that thread it doesn't show much. I'm also just using
sql
template directive with the CTE
Maybe there could be a solution using the core api
Also a very, very wierd behavior
I can't get inference from the sql<OrderProduct>
It infers data as unknown
the execute
method should pick up the type from the sql<T>
Do you have anything in mind why this would happen?
Am I doing something wrong? š®rphlmr ā”ā¢246d ago
I can reproduce. Not sure if it is expected š§
Guilherme Rosadoā¢246d ago
š¢
@Andrew Sherman Is this a bug?
Angelelzā¢246d ago
It seems this query could be written like this:
I'm assuming a lot of table a column names here. So you might need to edit it
btw, you might want to do this in a transaction
rphlmr ā”ā¢246d ago
I thought the same but not sure if running them separately (not in the same execute) will produce the same result
Angelelzā¢246d ago
db.$with
, only sets up the with for drizzle, it doesn't send anything to the DB.
I forgot to await the db call. Will editGuilherme Rosadoā¢246d ago
You're doing two queries here
Isn't real 1-1 to the query I stated above
I think this might be a limitation of drizzle?
I mean, if I understood correctly.
Angelelzā¢246d ago
Then I don't think I understood your query. Can you write it in plain SQL? cause I believe you might have just put the what you would put in the sql operator
The values from the insert are coming from the select?
Guilherme Rosadoā¢246d ago
Yep!
Angelelzā¢246d ago
Oh, my bad
Guilherme Rosadoā¢246d ago
The select is just to format the data so that the input can accept it
I do it all the time
But sometimes this tricks get super hard to implement in drizzle xD
Sql is just boom, done. There's lot of mental effort to get this right
Maybe I just lack examples
Angelelzā¢246d ago
I think this might work, I just don't have anywhere to test it right now
The types might complain though
Guilherme Rosadoā¢246d ago
Oh okay
So basically you passed the CTE to below the insert
Yeah I didn't thought that way
Yeah that might work
Smart
Thanks!
Still I wonder if sometimes isn't just easier to write sql
Like
vs
Angelelzā¢246d ago
Well, you can always write SQL, but you always end up writing just text, no intelisense or type safety
That query should have correct types
BTW, I'm hoping that query works, I'm not 100% it does
Guilherme Rosadoā¢246d ago
Yeah the intelisense & typesafety is what is making me stay, but still that API takes a lot of mental effort to adopt š¢ . There's a bit of a learning curve, even if you're actually cool on the sql
I think it needs to improve a little, rn it's a bit confusing. At least for me.
Thanks so much for the effort @angelelz
Will check it out
Angelelzā¢246d ago
I honestly think it's quite the opposite. What I've seen is people without SQL experience struggle with the crud API from Drizzle. You can see how you can translate from SQL to Drizzle syntax directly. There are changes we have to do to translate it to JS/TS like the eq operator has to be a function and it's written before the 2 operands, but there is no other way to express that in JS.
I this example you can see how the
db.$with
just saves the cte query in a Drizzle data structure for use in the actual select, very similar to how you would do it in SQLGuilherme Rosadoā¢246d ago
Yeah but it's a lot more verbose
But I get your point š
Not hating on the tool, just that sometimes I know exactly what I want in sql, but translating to drizzle is a little hard
One example
To achieve this in drizzle it's not obvious
I need to
It's very wierd
Or is there another way of doing it?
Angelelzā¢246d ago
Uf
If you do it like that, the type is going to be wrong
Angelelzā¢246d ago
Angelelzā¢246d ago
I suggest you do the subqueries as separate variables
More readable
Guilherme Rosadoā¢245d ago
The type is not going to be wrong
I won't retrieve col_1 or col_2
They are just for ordering and other specific use cases
Maybe I should have mentioned that
And the subquery here is not an option...
I'm just creating columns here, I can decide If I want retrieve that data to the select or not
It would be cool for the from accepted an array, that way I could create as much columns as I wanted, like this:
This would make a lottttt of sense
And would make it possible for me
These are just my takes for the first time using the library.
At the beginning looked awesome, but now there's a bit of an effort...
Maybe I'm doing it wrong XD
Angelelzā¢245d ago
The "Maybe I'm doing it wrong" is usually a sign the the library we're using is missing some features
Drizzle is not yet 1 to 1 fully compatible with SQL
It's not v1 yet
That's why we have the sql operator for all the stuff that we're missing
Guilherme Rosadoā¢245d ago
True š. Well, maybe my rant here may have brought ideas
Got it!
Angelelzā¢245d ago
Can you open an issue in github for the feature you're referring to?
Guilherme Rosadoā¢245d ago
Sure, I'll do it
šŖ
Angelelzā¢245d ago
Now, let me ask you a question
Is the query you're trying to build possible with joins?
Guilherme Rosadoā¢245d ago
Uh yeah I could just join and select inside the join, acting like a table
That's smart
Hmmmm
Angelelzā¢245d ago
Our join api is pretty good, type safe
And you can also use the relations API
Guilherme Rosadoā¢245d ago
It's not possible, drizzle only has available 3 types of joins
Wait
I could do that with UNION
Not join
Because some columns I just need to calculate shit on the side
Angelelzā¢245d ago
Lol, we don't suppor unions yet
Guilherme Rosadoā¢245d ago
E.g ranking etc
Then I would filter the rows based on the results of those columns
I think I could use joins after all, if those columns had some ID that where I could do
(on(table.id, col_from_specific_select.id))
Angelelzā¢245d ago
I have an open draft PR to add union to drizzle
Angelelzā¢245d ago
Guilherme Rosadoā¢245d ago
Nice man!
Looking good
I'll keep digging, I'll try to come-up with some different solutions
You may have given some ideas that made me think of different solutions
Much thanks
Angelelzā¢245d ago
I believe joining tables is faster than selecting from several selects
I might be wrong
Now I want to do some explain analize lol
Guilherme Rosadoā¢245d ago
I don't think so, the join still needs to compare every result and align with the ID of different tables (or subqueries acting as a table)
Not sure tho
ahahahahah
Great question
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
7.4KMembers
View on DiscordWant results from more Discord servers?
More PostsAdd drizzle-kit check as a pre-commit hookIs there a way to add drizzle-kit check as a pre-commit hook with husky that would actually prevent Planetscale swapped the order of Primary Key RelationHey, running into this weird issue when using Drizzle push on a staging branch of Planetscale, tryinPostgres functions in the ORM?Hello everyone, I'm learning typescript and SQL (with drizzle) and I'm trying to make an api endpoinInferModelFromColumns with columns defined with sql``Let's say I have a select list that looks like this
```ts
const defaultBookFields = {
id: books.Issue with drizzle-zod?Schema
```
export const testinArray = pgTable("testing_array", {
id: varchar("id", { length: 14 Optional One-to-One RelationCurrently it's possible to define only a One-to-One relation (https://orm.drizzle.team/docs/rqb#one-Having Issues Pushing Database SchemaHi I am getting this issue whenever I am trying to push the database schema. Any help would be greatmigration failedcode:
```js
const migrationClient = postgres("postgresql://jer:admin@localhost:5432/test", { max: 1TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')I am attempting to run db push, but I am getting this error above^^
does anyone know what might be Nesting sqlite drizzle into a deeper location in my ts app, difficulties with src/ vs dist/Hey! I'm very new to drizzle and trying to set up a very small drizzle instance for a service that rsyntax highlighting not workingI reinstalled the node_modules multiple times and it didnt fix my problem. This isnt suposed to lookPrepare raw sql query (Postgres)Hi, I need to do a recursive query, so I think I need to use the magic sql operator to build it. Is packing migration files with the libraryhey there, I'm trying to use `migrate` inside from a library consumer (i.e I have a `@mycompany/db-lAny way to invoke a SQL function?For ex: lets say I declare a SQL function `CREATE OR REPLACE FUNCTION do_something()`
how do i invoHow to include array literals in raw sql?I have an input array used for an order by using the array_position function - rather than passing tMySQL Table SchemasI need to generate the following structure of tables:
`1. Sales
2.Sales
....`
Currently if I use wany way to automatically cast numeric types on retrieval?I have a numeric field in a video table:
```
export const VideoTable = pgTable('videos', {
id: uuiQuery in drizzle doesn't work## Error:
```console
DrizzleError: No fields selected for table "usersToGroups" ("users_groups")
``Cannot read properties of undefined (reading 'referencedTable')Seemingly getting this error out of nowhere. I believe it's related to the relations I defined, but Relational query builder in mysql?I cant find anything about how to implement this. Am I blind or is this impossible?