© 2026 Hedgehog Software, LLC

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

foreign key mismatch

how do i create a unique constraint on
product_num
product_num
,
Store.store_num
Store.store_num
,
Store.chainName
Store.chainName


export const Store = sqliteTable(
    "Store",
    {
        id: text("id").primaryKey().unique(),
        store_num: text("store_num").notNull(),
        chain_name: text("chain_name").notNull(),
                ...
    },

    (t) => ({
        unique: unique().on(t.store_num, t.chain_name),
    })
);

export const Product = sqliteTable(
    "Product",
    {
        id: text("id").primaryKey().unique(),
        product_num: text("product_num").notNull(),
        store_num: text("store_num")
            .notNull()
            .references(() => Store.store_num),
        chainName: text("chainName")
            .notNull()
            .references(() => Store.chain_name),
                 ...
    },

    (t) => ({
        unique: unique().on(t.product_num, t.store_num, t.chainName), // how do i create a unique contraint on product_num, Store.store_num, Store.chainName
    })
);
export const Store = sqliteTable(
    "Store",
    {
        id: text("id").primaryKey().unique(),
        store_num: text("store_num").notNull(),
        chain_name: text("chain_name").notNull(),
                ...
    },

    (t) => ({
        unique: unique().on(t.store_num, t.chain_name),
    })
);

export const Product = sqliteTable(
    "Product",
    {
        id: text("id").primaryKey().unique(),
        product_num: text("product_num").notNull(),
        store_num: text("store_num")
            .notNull()
            .references(() => Store.store_num),
        chainName: text("chainName")
            .notNull()
            .references(() => Store.chain_name),
                 ...
    },

    (t) => ({
        unique: unique().on(t.product_num, t.store_num, t.chainName), // how do i create a unique contraint on product_num, Store.store_num, Store.chainName
    })
);

When i do
drizzle-kit push:sqlite
drizzle-kit push:sqlite

LibsqlError: SQLITE_UNKNOWN: SQLite error: foreign key mismatch - "Product" referencing "Store"
    at mapHranaError (file:///D:/../../node_modules/@libsql/client/lib-esm/hrana.js:257:16)
    at HttpClient.execute (file:///D:/../../node_modules/@libsql/client/lib-esm/http.js:56:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async TursoSqlite.run (D:\..\\node_modules\drizzle-kit\bin.cjs:25414:9)
    at async Command.<anonymous> (D:\gitpersonal\sg-backend\node_modules\drizzle-kit\bin.cjs:63360:9) {
  code: 'SQLITE_UNKNOWN',
  [cause]: [ResponseError: SQLite error: foreign key mismatch - "Product" referencing "Store"] {
      message: 'SQLite error: foreign key mismatch - "Product" referencing "Store"',
LibsqlError: SQLITE_UNKNOWN: SQLite error: foreign key mismatch - "Product" referencing "Store"
    at mapHranaError (file:///D:/../../node_modules/@libsql/client/lib-esm/hrana.js:257:16)
    at HttpClient.execute (file:///D:/../../node_modules/@libsql/client/lib-esm/http.js:56:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async TursoSqlite.run (D:\..\\node_modules\drizzle-kit\bin.cjs:25414:9)
    at async Command.<anonymous> (D:\gitpersonal\sg-backend\node_modules\drizzle-kit\bin.cjs:63360:9) {
  code: 'SQLITE_UNKNOWN',
  [cause]: [ResponseError: SQLite error: foreign key mismatch - "Product" referencing "Store"] {
      message: 'SQLite error: foreign key mismatch - "Product" referencing "Store"',
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

foreign key
Drizzle TeamDTDrizzle Team / help
3y ago
Schema is "wrong" resulting in "foreign key mismatch" error
Drizzle TeamDTDrizzle Team / help
2y ago
disambiguate foreign key relations
Drizzle TeamDTDrizzle Team / help
2y ago
Foreign Key - On Delete
Drizzle TeamDTDrizzle Team / help
3y ago