Replace on Insert

ZZen5/7/2023
I can see that there is a .ignore() option added to insert, but is there an equivalent .replace() or someway to flag it in the code? I'm doing an insert many by passing through an array of values (e.g. await db.insert(ActivityTable).values(values);) which is effectively an over write of existing data as well as new data
Mmachour5/7/2023
ZZen5/7/2023
Sorry I wasn't clear - I'm using mysql-core which doesn't have the onConflictDoUpdate chain though I'm again reminded how postgres is the superior database!
Mmachour5/7/2023
just discovered that all drivers don't implement all methods 😅 well, using MySQL that would be a REPLACE INTO .. I see a onDuplicateKeyUpdate for MySQL tho
ZZen5/9/2023
yup the REPLACE is what I'm used to using in SQL. I've never used ON DUPLICATE KEY UPDATE in SQL before, I'll check it out now - hopefully that's what I need! for reference onDuplicateKeyUpdate worked! My final code looked something like this:
db.insert(TestingTable)
.values(values)
.onDuplicateKeyUpdate({
set: {
fizz: sql`values(fizz)`,
buzz: sql`values(buzz)`,
fizzBuzz: sql`values(fuzz_buzz)`,
updatedAt: sql`values(updated_at)`,
},
});
db.insert(TestingTable)
.values(values)
.onDuplicateKeyUpdate({
set: {
fizz: sql`values(fizz)`,
buzz: sql`values(buzz)`,
fizzBuzz: sql`values(fuzz_buzz)`,
updatedAt: sql`values(updated_at)`,
},
});

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Is there a way to modify the select() on an existing query ?Let's say I have this query that I pass to a function ```ts const query = db.select().from(myTable)The inferred type of '<tableName>' cannot be named without a reference to '.pnpm/[email protected]I'm trying to use the `drizzle-zod` but I keep getting this error... Any idea on where it comes froMigration failure on fresh DBI'm currently experiencing a migration failure on a fresh Postgres DB with both postgres.js and nodeUsing Vercel Postgres and developing locallyTo confirm, if I want to use Vercel Postgres in production and a local Postgres DB for development, Custom Type interpreted as StringHello everyone, I am having some issues with getting Drizzle to work with PostGIS, I am trying to haCan I use queryBuilder for inserts?My scenario is that I want to read some rows from a database (prod), and generate a SQL file that weI ran introspect:pg to initialize my schema, then created a new migration. How should I deploy this?The remote database, right now, doesn't have the `drizzle.__drizzle_migrations` table. It'd be nice Transaction rollbackHello guys. Kind of dumb question but should I cover body of transaction in try/catch and call rollberror creating relationshipI'm having trouble creating a relationship in MySQL using drizzle-orm, I'm running `npx drizzle-kit define default value for arrayI work with a PostgreSQL database. Here is my Schema: export const user = pgTable( 'auth_user',TS query types dont match + no return typesI'm trying to query data from my db after importing the Item table from my schema. `const data = awaCan't generate migrations because of top level awaitThis only recently started happening.Here is my `drizzle.config.json` : ```{ "out": "./migratError types with custom schemaHi, I'm trying to do a general function to paginate my querys, but I'm having some problems with theTrying to write next-auth adapterblahblahblahrober AKA @Dan Kochetov helped me get most of the way to having a next-auth adapter doneBeen getting this error while using the libsql client. The code works though. Any ideas. Thanks! 🙂I get the error from the screenshot every time I access my db client, but the code works fine thoughSelect wildcardI'm sure someone's asked this before but I can't find it. Is there not a way to do a `.select()` wiRangeError: Maximum call stack size exceeded - When I try to INSERTHi, I am getting `RangeError: Maximum call stack size exceeded` when I try to INSERT something in tTables are not being generatedHey 👋 I'm probably missing something obvious but with the setup I have (shown in the image) drizzlInserting records into related tablesI have a few of tables that are related. I just need to know if there's a better approach when inserinArrayFor what i've seen pass an empty array to inArray is not supported, how to you handle this cases? us