question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MySQL does not support `onDelete: setDefault`

See original GitHub issue

When trying to migrate a super simple schema to MySQL with an onDelete: setDefault, this happens:

C:\Users\Jan\Documents\throwaway\setdefault>npx prisma db push     
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db": MySQL database "purple_kingfisher" at "mysql-db-provision.cm0mkpwj8arx.eu-central-1.rds.amazonaws.com:3306"
Error: Cannot add foreign key constraint
   0: sql_migration_connector::sql_database_step_applier::apply_migration
             at migration-engine\connectors\sql-migration-connector\src\sql_database_step_applier.rs:11
   1: migration_core::api::SchemaPush
             at migration-engine\core\src\api.rs:187
model OnDeleteSetDefaultParent {
  id                Int                                @id @default(autoincrement())
  name              String                             @unique
  mandatoryChildren OnDeleteSetDefaultMandatoryChild[]
}

model OnDeleteSetDefaultMandatoryChild {
  id       Int                      @id @default(autoincrement())
  name     String                   @unique
  parent   OnDeleteSetDefaultParent @relation(fields: [parentId], references: [id], onDelete: SetDefault)
  parentId Int                      @default(1)
}

Per our documentation this should fundamentally work (if maybe a bit different than expected): image

But looking at the MySQL docs, this does not seem to be supported at all:

SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.

Similar for MariaDB:

The SET DEFAULT action is not supported.

We even mention similar in our engines comments: https://github.com/prisma/prisma-engines/blob/ccf3dc944acdabb431947150e12b984b34c538cd/query-engine/connector-test-kit-rs/query-engine-tests/tests/new/ref_actions/on_delete/set_default.rs#L1 https://github.com/prisma/prisma-engines/blob/ccf3dc944acdabb431947150e12b984b34c538cd/migration-engine/migration-engine-tests/tests/migrations/relations.rs#L565-L566

We should adapt our validation to not allow this, and update our documentation afterwards as well. No need for our users to waste their time with this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
jkomynocommented, Nov 6, 2022

I have summarised my findings in this internal notion doc.

1reaction
jkomynocommented, Nov 3, 2022

I have just found out that MySQL 8.0 docs report the following:

SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InnoDB mySQL unable to set "ON DELETE SET DEFAULT ...
While SET DEFAULT is allowed by the MySQL Server, it is rejected as invalid by InnoDB. CREATE TABLE and ALTER TABLE statements using...
Read more >
13.1.20.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
A foreign key constraint on a stored generated column cannot use CASCADE , SET NULL , or SET DEFAULT as ON UPDATE referential...
Read more >
Good explanation of cascade (ON DELETE/UPDATE) behavior
ON DELETE SET DEFAULT : if a row of the referenced table is deleted, then all referencing columns in all matching rows of...
Read more >
SQLite Foreign Key Support
For an "ON DELETE CASCADE" action, this means that each row in the child table that was associated with the deleted parent row...
Read more >
1.7.3.2 FOREIGN KEY Constraints - Oracle Help Center
SET DEFAULT is also supported by the MySQL Server but is currently rejected as invalid by InnoDB . Since MySQL does not support...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found