Repository navigation
Description
The allsquare database schema example has a datetime with a default, and via Introspection and then Migrations this breaks in some circumstances.
This is the original SQL:
--
-- Table structure for table `albums`
--
DROP TABLE IF EXISTS `albums`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `albums` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`albumable_type` varchar(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`albumable_id` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`cover_picture_id` int(11) DEFAULT NULL,
`priority` int(11) DEFAULT NULL,
`media_id` int(11) DEFAULT NULL,
`attachment_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_albums_on_albumable_type_and_albumable_id` (`albumable_type`,`albumable_id`) USING BTREE,
KEY `index_albums_on_deleted_at` (`deleted_at`)
) ENGINE=InnoDB AUTO_INCREMENT=177178 DEFAULT CHARSET=utf8mb4;
/*!40101 SET character_set_client = @saved_cs_client */;From our Introspection databases, this gets turned into 2 different (!) Prisma schemas for MySQL and MariaDB:
MySQL:
model albums {
id Int @id @default(autoincrement())
title String? @db.VarChar(255)
content String? @db.Text
albumable_type String?
albumable_id Int?
created_at DateTime @db.DateTime(0)
updated_at DateTime @db.DateTime(0)
deleted_at DateTime @default(dbgenerated("1970-01-01 00:00:00")) @db.DateTime(0)
type String? @db.VarChar(255)
cover_picture_id Int?
priority Int?
media_id Int?
attachment_id Int?
@@index([albumable_type, albumable_id], map: "index_albums_on_albumable_type_and_albumable_id")
@@index([deleted_at], map: "index_albums_on_deleted_at")
}MariaDB:
model albums {
id Int @id @default(autoincrement())
title String? @db.VarChar(255)
content String? @db.Text
albumable_type String?
albumable_id Int?
created_at DateTime @db.DateTime(0)
updated_at DateTime @db.DateTime(0)
deleted_at DateTime @default(dbgenerated("('1970-01-01 00:00:00')")) @db.DateTime(0)
type String? @db.VarChar(255)
cover_picture_id Int?
priority Int?
media_id Int?
attachment_id Int?
@@index([albumable_type, albumable_id], map: "index_albums_on_albumable_type_and_albumable_id")
@@index([deleted_at], map: "index_albums_on_deleted_at")
}To highlight the difference:
- deleted_at DateTime @default(dbgenerated("1970-01-01 00:00:00")) @db.DateTime(0)
+ deleted_at DateTime @default(dbgenerated("('1970-01-01 00:00:00')")) @db.DateTime(0)See how the MariaDB dbgenerated has an additional ('...') in there.
This leads to two different .sql migration files to be created as well:
MySQL:
-- CreateTable
CREATE TABLE `albums` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NULL,
`content` TEXT NULL,
`albumable_type` VARCHAR(191) NULL,
`albumable_id` INTEGER NULL,
`created_at` DATETIME(0) NOT NULL,
`updated_at` DATETIME(0) NOT NULL,
`deleted_at` DATETIME(0) NOT NULL DEFAULT 1970-01-01 00:00:00,
`type` VARCHAR(255) NULL,
`cover_picture_id` INTEGER NULL,
`priority` INTEGER NULL,
`media_id` INTEGER NULL,
`attachment_id` INTEGER NULL,
INDEX `index_albums_on_albumable_type_and_albumable_id`(`albumable_type`, `albumable_id`),
INDEX `index_albums_on_deleted_at`(`deleted_at`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;MariaDB:
-- CreateTable
CREATE TABLE `albums` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NULL,
`content` TEXT NULL,
`albumable_type` VARCHAR(191) NULL,
`albumable_id` INTEGER NULL,
`created_at` DATETIME(0) NOT NULL,
`updated_at` DATETIME(0) NOT NULL,
`deleted_at` DATETIME(0) NOT NULL DEFAULT ('1970-01-01 00:00:00'),
`type` VARCHAR(255) NULL,
`cover_picture_id` INTEGER NULL,
`priority` INTEGER NULL,
`media_id` INTEGER NULL,
`attachment_id` INTEGER NULL,
INDEX `index_albums_on_albumable_type_and_albumable_id`(`albumable_type`, `albumable_id`),
INDEX `index_albums_on_deleted_at`(`deleted_at`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;While the MariaDB SQL can be executed successfully, the MySQL one unfortunately fails with this error message:
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: normalized_migration_name
Database error code: 1064
Database error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-01-01 00:00:00,
`type` VARCHAR(255) NULL,
`cover_picture_id` INTEGER NU' at line 10
Please check the query number 4 from the migration file.
Note the difference between the original, and the new MySQL statement for deleted_at:
- `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00', -- original
+ `deleted_at` DATETIME(0) NOT NULL DEFAULT 1970-01-01 00:00:00, -- MySQL via Migrate
+ `deleted_at` DATETIME(0) NOT NULL DEFAULT ('1970-01-01 00:00:00'), -- MariaDB via MigrateTLDR
Introspection picks up the default from MySQL into dbgenerated in a way that Migrate creates SQL that can not be executed, as it is missing the quotes around the date string that should be the default.
Activity
pantharshit00 commentedon Nov 26, 2021
I can reproduce this.
janpio commentedon Nov 26, 2021
Internal note: This is also reason #1 for failing MySQL Migrations in our internal Migrations-CI.
Default documentation:
janpio commentedon Dec 21, 2021
Follow up, internal: https://www.notion.so/prismaio/Better-representing-Date-Time-and-Datetime-Default-Values-in-the-PSL-b754d2cb84a54bebb876327bc4f51d2b
RizeKishimaro commentedon Oct 21, 2023
i thought i was the only who have this error but why prisma generate DATETIME(3) in the mysql statement?
Error Logs:
`Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": MySQL database "ivr_tarot" at "localhost:3306"
Error: P3006
Migration
20230825044452_initfailed to apply cleanly to the shadow database.Error code: P3018
Error:
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20230825044452_init
Database error code: 1064
Database error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '3),
updated_atDATETIME(3) NOT NULL,) DEFAULT CHA' at line 7
Please check the query number 1 from the migration file.
`
but the query that generated by the prisma is
CREATE TABLEPhoneNumberTable(idINTEGER NOT NULL AUTO_INCREMENT,phone_numberINTEGER NOT NULL,nameVARCHAR(191) NOT NULL,aspect_idINTEGER NOT NULL,created_atDATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),updated_at` DATETIME(3) NOT NULL,) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
`
and prisma model was
model tarot_card_category {
id Int @id @default(autoincrement())
name String
dial_number Int
status Status
tarot_card tarot_card[]
created_at DateTime @default(now())
updated_at DateTime @updatedat
}`
can anyone help me with this error?
`