clxxiii
clxxiii
DTDrizzle Team
Created by kal on 4/11/2024 in #help
How to reference composite key in foreign key
oh yea i knew I forgot something: Just like with a single foreign key, you have to make sure that only one row in the table can have that value (unique) With a composite foreign key, you need to make sure there is no row that could share, in your case, both the game id and the turn. (composite unique) Look to the docs for more information:
...
export const composite = pgTable('composite_example', {
id: integer('id'),
name: text('name'),
}, (t) => ({
unq: unique().on(t.id, t.name), // Let drizzle decide the name
unq2: unique('custom_name').on(t.id, t.name) // Specify a name
}));
...
...
export const composite = pgTable('composite_example', {
id: integer('id'),
name: text('name'),
}, (t) => ({
unq: unique().on(t.id, t.name), // Let drizzle decide the name
unq2: unique('custom_name').on(t.id, t.name) // Specify a name
}));
...
6 replies
DTDrizzle Team
Created by kal on 4/11/2024 in #help
How to reference composite key in foreign key
I'm not totally sure, (ie dont copy and paste this in) but hopefully something like this points you in the right direction
export const moveTimestamps = pgTable("moveTimestamps", {
move_game_id: varchar("move").notNull(),
move_turn: integer("turn").notNull.unique()
}, (t) => ({
pk: primaryKey({ columns: [t.move_game_id, t.move_turn]}),
fk: foreignKey({
columns: [t.move_game_id, t.move_turn],
foreignColumns: [moves.gameID, moves.turn]
})
}))
export const moveTimestamps = pgTable("moveTimestamps", {
move_game_id: varchar("move").notNull(),
move_turn: integer("turn").notNull.unique()
}, (t) => ({
pk: primaryKey({ columns: [t.move_game_id, t.move_turn]}),
fk: foreignKey({
columns: [t.move_game_id, t.move_turn],
foreignColumns: [moves.gameID, moves.turn]
})
}))
have not tested this snippet either
6 replies