-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
UPDATE
As of 27.10.2022, the details in this issue are outdated.
Refer to https://www.prisma.io/docs/guides/database/using-prisma-with-planetscale for the latest instructions on using Prisma with PlanetScale.
Outdated Issue content
PlanetScale is a new and interesting cloud database based on MySQL. It's exciting!
PlanetScale is a bit different than other MySQL database providers though:
- No support for foreign keys. This is challenging for Prisma users, as Prisma by default uses foreign keys to express relations.
- No creation of additional databases using
CREATE DATABASEbut need to use their tooling (web UI or CLI). This can be challenging, as some development commands of Prisma likeprisma migrate devcreate temporary databases in the background. - No schema changes on your production database (but make them on branches, then merge the schema changes back). This is challenging for Prisma users, as usually they run Migrations against their database directly via
prisma migrate deployorprisma db push.
Here is how you can use Prisma and PlanetScale together anyway:
-
No foreign keys / No Prisma Migrate (
migrate devanddb push)- Problem: PlanetScale does not allow foreign keys in the database schema (
ERROR 70100 (1317): foreign key constraints are not allowed, see https://code.openark.org/blog/mysql/the-problem-with-mysql-foreign-key-constraints-in-online-schema-changes), which Prisma relies on for relations. - Starting with 2.27.0 Prisma catches the error and outputs a helpful error message (that currently redirects here)
- Workaround starting with 3.1.1: Use the preview feature
referentialIntegrityand the datasource propertlyreferentialIntegrity = "prisma"to enable a mode that automatically leaves out foreign keys in migrations:You can now properly usegenerator client { provider = "prisma-client-js" previewFeatures = ["referentialIntegrity"] } datasource db { provider = "mysql" url = env("DATABASE_URL") shadowDatabaseUrl = env("SHADOW_DATABASE_URL") referentialIntegrity = "prisma" } ...prisma migrate devandprisma db push✅
(Between 2.24.0 and 3.1.1 the setting was calledplanetScaleMode = trueand hidden behind the preview feature flagplanetScaleMode)
- Problem: PlanetScale does not allow foreign keys in the database schema (
-
If you are using
migrate devto migrate: NoCREATE DATABASE/ No automatic shadow database creation- Problem: PlanetScale does not allow creating new databases with
CREATE DATABASE, which Prisma Migrate prefers to use for the shadow database of Prisma Migrate. - Solution: Create a branch
shadowor similar and open put its connection string asshadowDatabaseof yourdatasourceinschema.prisma✅datasource db { provider = "mysql" url = env("DATABASE_URL") shadowDatabaseUrl = env("SHADOW_DATABASE_URL") referentialIntegrity = "prisma" } - Potential improvement: Catch error thrown if you try anyway and output better error message with link to documentation.
- Problem: PlanetScale does not allow creating new databases with
-
If you are using
migrate devto migrate: Prisma migration table is not copied tomainby default- PlanetScale now has a setting "Automatic schema migrations" that enables this behavior:

As you can seePrismais one of the default options, and enabling this option and choosingPrismamakes sure that the content of the migration table_prisma_migrationsis copied from the branch tomainwith the deploy request. 🥳
- PlanetScale now has a setting "Automatic schema migrations" that enables this behavior:
-
No schema changes on production branches
- Problem: PlanetScale does not allow schema changes on the production branch of the database (
ERROR HY000 (1105): direct DDL is disabled), which Prisma Migrate tries to do if you tell it to. - Starting with 2.27.0 Prisma catches the error and outputs a helpful error message (that currently redirects here)
- Solution: Only execute Prisma Migrate on non production branches, then use a PlanetScale "deploy request" to merge the schema changes to your
main✅
- Problem: PlanetScale does not allow schema changes on the production branch of the database (
We are very excited for you to try PlanetScale with Prisma. If you hit any bumps, let us know either here or in new issues or discussions in our repository. Thanks!