Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Unable to create MySQL foreign string keys #4221

Open
1 task done
ElPrudi opened this issue Mar 6, 2025 · 0 comments
Open
1 task done

[BUG]: Unable to create MySQL foreign string keys #4221

ElPrudi opened this issue Mar 6, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@ElPrudi
Copy link

ElPrudi commented Mar 6, 2025

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.40.0

What version of drizzle-kit are you using?

0.35.0

Other packages

mysql2 3.13.0

Describe the Bug

While number foreign keys work (int().autoincrement()...), applying the same thing to char, varchar or text will result in this error:

Error: Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'Note_user_id_User_id_fk' are incompatible.
    at PromiseConnection.execute (<project>/node_modules/.pnpm/mysql2@3.13.0/node_modules/mysql2/lib/promise/connection.js:47:22)
    at Object.query (<project>/node_modules/.pnpm/drizzle-kit@0.30.5/node_modules/drizzle-kit/bin.cjs:78651:40)
    at mysqlPush (<project>/node_modules/.pnpm/drizzle-kit@0.30.5/node_modules/drizzle-kit/bin.cjs:81763:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Object.handler (<project>/node_modules/.pnpm/drizzle-kit@0.30.5/node_modules/drizzle-kit/bin.cjs:92124:9)
    at async run (<project>/node_modules/.pnpm/drizzle-kit@0.30.5/node_modules/drizzle-kit/bin.cjs:91395:7) {
  code: 'ER_FK_INCOMPATIBLE_COLUMNS',
  errno: 3780,
  sql: 'ALTER TABLE `Note` MODIFY COLUMN `user_id` varchar(24) COLLATE ascii_bin NOT NULL;',
  sqlState: 'HY000',
  sqlMessage: "Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'Note_user_id_User_id_fk' are incompatible."
}

My drizzle schemas:

import { mysqlTable char } from 'drizzle-orm/mysql-core'
import { createId } from '@paralleldrive/cuid2'

function id() {
  return char('id', { length: 24 }).primaryKey().$defaultFn(createId)
}

export const user = mysqlTable(
  'User',
  {
    id: id()
  }
)

export const note = mysqlTable(
  'Note',
  {
    id: id(),
    userId: char('user_id', { length: 24 }).references(() => user.id).notNull()
  }
)

The generated SQL also gives no clue on why it fails, because all values are char(24) NOT NULL. I tried collating found here, but this also didn't work.

@ElPrudi ElPrudi added the bug Something isn't working label Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant