Replies: 4 comments
|
Schema-level collation is not supported in Prisma today — that is exactly what the linked issues (#29171, #14179) are tracking. While that is open, a few practical workarounds: 1. Edit the migration SQL directly. 2. Set the database default. If you want one collation for everything new, set it at the database level: CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;Every new table/column then inherits that collation automatically, so your Prisma schema does not need to know about it. 3. Raw SQL for one-offs. For converting existing tables, For prioritization: discussions on Prisma do not get the same triage visibility as issues, so 👍 reactions on issue #29171 are the strongest signal you can give the team that you want this in the schema language. |
|
As far as I know, Prisma still doesn't have a top-level schema setting for "default collation for everything generated from this schema". The practical workarounds are:
That generally works fine with Prisma, but the source of truth for the default collation stays in SQL / database config rather than |
|
This is a genuinely painful limitation that keeps coming up across multiple issues and discussions, so it is worth consolidating the community position clearly. The core problem Prisma hardcodes The specific scenario that breaks things repeatedly is integrating Prisma into an existing database or alongside another ORM or service. If the existing tables use What would actually fix this A datasource db {
provider = "mysql"
url = env("DATABASE_URL")
collation = "utf8mb4_0900_ai_ci"
}A field-level override would cover the remaining cases: model Post {
title String @db.VarChar(255) @collation("utf8mb4_unicode_ci")
}Current workaround The only reliable workaround right now is a post-migration SQL script that alters affected tables. You can hook into this using Prisma's ALTER TABLE Post CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
ALTER TABLE User CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;This is obviously not sustainable at scale but it unblocks you in the short term. Ask for the Prisma team Given that this has been raised in #4743, #14179, and #29171 spanning several years, it would be helpful to get an official position on whether this is on the roadmap and at what priority. Even a confirmed "not planned" allows the community to make informed decisions about workarounds or alternative tooling. The absence of any response is the most frustrating outcome for a frequently requested feature. Adding a thumbs up to #29171 directly will help signal priority to the team. |
|
Just to set expectations on the current state: there is no way to express collation in the Prisma schema language today. It isn't in the schema reference — there's no Two practical ways to get the collation you actually want: 1. Edit the generated migration SQL ( npx prisma migrate dev --create-onlyThen open the new file under ALTER TABLE `MyTable` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;Apply it with 2. Set the default collation on the database/server so new tables inherit it. Since the mismatch is between Prisma's ALTER DATABASE `mydb` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;A column with no explicit Option 2 is usually the cleaner fix for the "integrating new services into the same database" case you described, since it makes Prisma-managed tables consistent with everything else by default; option 1 is there when you need a specific collation on specific columns. |
|
This is a long-standing, well-documented gap, and you're right that it's gotten surprisingly little traction given how often it comes up. The good news is there's a working manual workaround today; the bad news is there's currently no schema-level config option, and this has been requested for years without being implemented. History of this requestThis isn't a new ask — it traces back further than you may realize:
So #29171 is at minimum the fourth time this exact request has been raised in some form across multiple years. That's worth noting in your issue as evidence of sustained demand, since maintainers triage partly by how often something recurs. The current workaround (works today, no plugin or core change needed)The workaround the Prisma team has pointed people to is: set the database's default collation to your desired value (e.g. Step-by-step for your case:
ALTER DATABASE your_database_name
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci; -- or whatever collation your other services use
npx prisma migrate dev --create-only
-- Prisma generates:
CREATE TABLE `User` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Edit to:
CREATE TABLE `User` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4;
npx prisma migrate devThis is workable but exactly as tedious as you'd expect — you must remember to do it on every single migration that creates a table, indefinitely, since there's no schema-level switch to make it automatic. A related but distinct issue worth being aware ofIf you're hitting collation mismatches specifically around foreign keys and joins (not just table creation), be aware there's a separate, nastier failure mode: manually adjusting the character/collation set in a migration can cause the table creation to silently fail with no error when a subsequent What I'd recommend doing with this reportSince #29171 already exists and you've cross-referenced #14179 and the #4743 discussion, the most useful next step is consolidating rather than spreading the conversation across yet more threads:
Hope this helps in the meantime — the manual workaround above should at least get you unblocked for new tables while the feature request sits in the backlog. |
Uh oh!
There was an error while loading. Please reload this page.
Question
Hi Prisma team and community,
I noticed that Issue #29171 (“Default MySQL collation for new tables/columns”) hasn’t received feedback.
This feature is important because:
It affects consistency across new tables/columns.
Developers often need explicit control over collation for multilingual or case-sensitive applications.
Other ORMs provide schema-level collation options, so adding this would improve parity.
This has also been discussed here: #14179 and #4743
I’d love to hear from both the Prisma team and the wider community:
How to reproduce (optional)
In MySQL, when new models are added to the schema file and migrated, Prisma generates SQL queries using the utf8mb4 character set and utf8mb4_unicode_ci collation. When integrating new services to the same database, there is a collation matching error due to mismatched collation
Expected behavior (optional)
Allow us to specify the default-collation, so that there wouldn't be mismatched collation
Information about Prisma Schema, Client Queries and Environment (optional)
No response
All reactions