Skip to content

MySQL/MariaDB: @default(dbgenerated(...)) semi broken for dates #10441

Open

Description

@janpio

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 Migrate

TLDR

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

  1. added
    bug/1-unconfirmed
    kind/bug
    on Nov 25, 2021
  2. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Nov 26, 2021
  3. pantharshit00 commented on Nov 26, 2021

    @pantharshit00
    Contributor

    I can reproduce this.

  4. janpio commented on Nov 26, 2021

    @janpio
    ContributorAuthor
  5. added this to the 3.7.0 milestone on Dec 1, 2021
  6. self-assigned this
    on Dec 8, 2021
  7. added
    tech/engines
    on Mar 24, 2022
  8. RizeKishimaro commented on Oct 21, 2023

    @RizeKishimaro

    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_init failed 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_at DATETIME(3) NOT NULL,

    PRIMARY KEY (`id`)
    

    ) 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 TABLE PhoneNumberTable(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,

    PRIMARY KEY (`id`)
    

    ) 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?
    `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtech/enginestopic: dates / DateTimetopic: introspectiontopic: mariadbtopic: mysql

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions