© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
5 replies
kal

How to reference composite key in foreign key

I'm trying to create a foreign key which points to another tables composite primary key. I cannot seem to find anywhere how this is meant to be achieved.

I have attached a simplified version of the code I am trying to get to work. As you can see in the
moves
moves
table I am creating a composite primary key. How do I then reference that in the
moveTimestamps
moveTimestamps
table. Also, what should I put in place of
varchar
varchar
in the
moveTimestamps
moveTimestamps
table
move
move
column, typically I would just match the data type as what it is referencing, but in thise case it should reference both an interger and a varchar.

const moves = pgTable(
  "moves",
  {
    gameID: varchar("game_id")
      .notNull()
      .references(() => games.id, { onDelete: "cascade" }),
    turn: integer("turn").notNull().unique(),  },
  (table) => {
    return {
      pk: primaryKey({ name: "id", columns: [table.gameID, table.turn] }),
    };
  },
);
const moves = pgTable(
  "moves",
  {
    gameID: varchar("game_id")
      .notNull()
      .references(() => games.id, { onDelete: "cascade" }),
    turn: integer("turn").notNull().unique(),  },
  (table) => {
    return {
      pk: primaryKey({ name: "id", columns: [table.gameID, table.turn] }),
    };
  },
);


export const moveTimestamps = pgTable("moveTimestamps", {
  move: varchar("move")
    .notNull()
    .references(() => //!What do i put here!)
    .primaryKey(),
});
export const moveTimestamps = pgTable("moveTimestamps", {
  move: varchar("move")
    .notNull()
    .references(() => //!What do i put here!)
    .primaryKey(),
});
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to reference to composite primary key
Drizzle TeamDTDrizzle Team / help
9mo ago
reference composite primary key
Drizzle TeamDTDrizzle Team / help
3y ago
Composite Primary Key from Foreign Keys
Drizzle TeamDTDrizzle Team / help
3y ago
How do I define a composite foreign key?
Drizzle TeamDTDrizzle Team / help
3y ago