drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.56k stars 489 forks source link

Support `INSERT INTO table SELECT ...` #398

Open dankochetov opened 1 year ago

ryoppippi commented 9 months ago

Any updates on this?

risseraka commented 8 months ago

Hi there,

Strong +1 on this enhencement.

Indeed, this kind of request support would alleviate the need to pass values into the JS world and having it all contained in the SQL world instead.

Nowadays, we've got to do something like this:

const values = db.select({ a, b, c }).from(table).leftJoin(...)
db.insert().values(values);

whereas it would be ideal to have all of drizzle schema capabilities and write something like that:

db.insert().from(db.select({ a, b, c }).from(table.leftJoin(...))

Cheers.

mytabworks commented 7 months ago

I am trying to achieve to insert the fund.current_balance to the fund_log.previous_balance using select query like the SQL below:

INSERT INTO Table1 (col1, col2, your_desired_value_from_select_clause, col3)
VALUES (
   'col1_value', 
   'col2_value',
   (SELECT col_Table2 FROM Table2 WHERE IdTable2 = 'your_satisfied_value_for_col_Table2_selected'),
   'col3_value'
);

I was trying to do it on drizzle by this method below:

const fundCurrent = db.select({ current_balance: fund.current_balance }).from(fund).where(eq(fund.id, id))
db.insert(fund_log).values({previous_balance: fundCurrent})

what am I doing wrong here?

MorelSerge commented 2 weeks ago

Seeing this issue is quite old, yet quite requested. Has there been any update from the Drizzle team or on the PR from @L-Mario564 ?