import { sql } from "drizzle-orm";
import { foreignKey, pgSchema, pgTable, unique, uuid, type AnyPgColumn } from "drizzle-orm/pg-core";
export const drizzleTest = pgSchema("drizzle_test");
export const childInDrizzleTest = drizzleTest.table(
"child",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
},
(table) => {
return {
testKey: foreignKey({
columns: [table.id, table.otherId],
foreignColumns: [parentInDrizzleTest.otherId, parentInDrizzleTest.childId],
name: "test_key",
}).onDelete("cascade"),
};
},
);
export const parentInDrizzleTest = drizzleTest.table(
"parent",
{
id: uuid("id")
.default(sql`uuid_generate_v4()`)
.primaryKey()
.notNull(),
otherId: uuid("other_id").notNull(),
childId: uuid("child_id"),
},
(table) => {
return {
parentChildIdFkey: foreignKey({
columns: [table.childId],
foreignColumns: [childInDrizzleTest.id],
name: "parent_child_id_fkey",
}).onDelete("restrict"),
parentOtherIdChildIdKey: unique("parent_other_id_child_id_key").on(
table.otherId,
table.childId,
),
parentChildIdKey: unique("parent_child_id_key").on(table.childId),
};
},
);