update multiple rows is not working. PostgresError: column excluded.name does not exist
I'm trying to insert multiple rows and update onconflict for those rows that have conflict.
this is my schema for order_phases
I wonder if I'm using excluded wrongly?
export type OrderPhases = {
restaurant_id: number;
uuid?: string;
name: string;
from: string;
to: string;
delivery_time: string;
};
export const updateRestaurantSettings = async (
orderPhaseValues: OrderPhases[],
deliveryLocationValues: { restaurant_id: number; location: string }[]
): Promise<void> => {
try {
await db.transaction(async (tx) => {
await db
.insert(order_phases)
.values(orderPhaseValues)
.onConflictDoUpdate({
target: order_phases.uuid,
set: {
name: sql.raw(`excluded.${order_phases.name.name}`),
from: sql.raw(`excluded.${order_phases.from.name}`),
to: sql.raw(`excluded.${order_phases.to.name}`),
delivery_time: sql.raw(`excluded.${order_phases.delivery_time.name}`),
},
});
await db.insert(delivery_location).values(deliveryLocationValues);
});
} catch (error) {
throw new Error("Error in updateRestaurant: " + error);
}
};export type OrderPhases = {
restaurant_id: number;
uuid?: string;
name: string;
from: string;
to: string;
delivery_time: string;
};
export const updateRestaurantSettings = async (
orderPhaseValues: OrderPhases[],
deliveryLocationValues: { restaurant_id: number; location: string }[]
): Promise<void> => {
try {
await db.transaction(async (tx) => {
await db
.insert(order_phases)
.values(orderPhaseValues)
.onConflictDoUpdate({
target: order_phases.uuid,
set: {
name: sql.raw(`excluded.${order_phases.name.name}`),
from: sql.raw(`excluded.${order_phases.from.name}`),
to: sql.raw(`excluded.${order_phases.to.name}`),
delivery_time: sql.raw(`excluded.${order_phases.delivery_time.name}`),
},
});
await db.insert(delivery_location).values(deliveryLocationValues);
});
} catch (error) {
throw new Error("Error in updateRestaurant: " + error);
}
};this is my schema for order_phases
export const order_phases = pgTable(
"Order_Phases",
{
id: serial("ID").notNull().primaryKey(),
restaurant_id: integer("Restaurant_ID")
.notNull()
.references(() => restaurants.id, { onDelete: "cascade" }),
uuid: uuid("UUID").defaultRandom().unique(),
name: text("Name").notNull(),
from: text("From").notNull(),
to: text("To").notNull(),
delivery_time: text("Delivery_Time").notNull(),
},
(t) => ({
unq: unique().on(t.restaurant_id, t.name),
})
);export const order_phases = pgTable(
"Order_Phases",
{
id: serial("ID").notNull().primaryKey(),
restaurant_id: integer("Restaurant_ID")
.notNull()
.references(() => restaurants.id, { onDelete: "cascade" }),
uuid: uuid("UUID").defaultRandom().unique(),
name: text("Name").notNull(),
from: text("From").notNull(),
to: text("To").notNull(),
delivery_time: text("Delivery_Time").notNull(),
},
(t) => ({
unq: unique().on(t.restaurant_id, t.name),
})
);I wonder if I'm using excluded wrongly?