Dylan
Dylan
DTDrizzle Team
Created by Dylan on 9/13/2023 in #help
OnConflictDoNothing with mysql
Hi everyone, I want to use the on conflict do nothing method described here: https://orm.drizzle.team/docs/insert#onconflict-and-upsert-insert-or-update. But it seems to be that only onConflictDoUpdate is available. Is this database specific or Am I doing something wrong here? this is my query:
await db.transaction(async (tx) => {
const storeId = nanoid();
await tx.insert(convenienceStores).values({
id: storeId,
name: store.storeName,
address: store.address,
brand: "brand",
coords: { lon: store.coords.lon, lat: store.coords.lat },
});

const newProducts = products.map(
(product) =>
({
id: nanoid(),
name: product.name ?? "NONAME",
mealSource: "STORE",
image: product.imageUrl,
calories: product.calories,
fat: product.fats,
carbs: product.carbs,
protein: product.proteins,
price: product.price,
} satisfies InferInsertModel<typeof meals>)
);

await tx.insert(meals).values(newProducts).onConflictDoNothing();
await tx.insert(convenienceStoresToMeals).values(
newProducts.map(
(meal) =>
({
convenienceStoreId: storeId,
mealId: meal.id,
} satisfies InferInsertModel<typeof convenienceStoresToMeals>)
)
);
});
await db.transaction(async (tx) => {
const storeId = nanoid();
await tx.insert(convenienceStores).values({
id: storeId,
name: store.storeName,
address: store.address,
brand: "brand",
coords: { lon: store.coords.lon, lat: store.coords.lat },
});

const newProducts = products.map(
(product) =>
({
id: nanoid(),
name: product.name ?? "NONAME",
mealSource: "STORE",
image: product.imageUrl,
calories: product.calories,
fat: product.fats,
carbs: product.carbs,
protein: product.proteins,
price: product.price,
} satisfies InferInsertModel<typeof meals>)
);

await tx.insert(meals).values(newProducts).onConflictDoNothing();
await tx.insert(convenienceStoresToMeals).values(
newProducts.map(
(meal) =>
({
convenienceStoreId: storeId,
mealId: meal.id,
} satisfies InferInsertModel<typeof convenienceStoresToMeals>)
)
);
});
6 replies