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.

Drift detected on first migration

See original GitHub issue

Bug description

Hi,

I’m getting issues when trying to setup prisma on an existing database (I’ve already setup prisma on a new db without any issues)

At first I thought it was from my existing db but I’ve tried on a simple one:

CREATE TABLE `collections` (
  `id` int NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` date NOT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `collections_slug_key` (`slug`),
  KEY `collection_slug` (`slug`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


INSERT INTO `collections` (`id`, `title`, `created_at`, `slug`)
VALUES
	(1, 'test', '2022-02-01', 'test');

How to reproduce

  1. Install the db (above)
  2. Run npx prisma db pull
  3. Run npx prisma generate
  4. Run first migration npx prisma migrate dev
  5. Got this message saying that I have to reset my db
Drift detected: Your database schema is not in sync with your migration history.

The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.

It should be understood as the set of changes to get from the expected schema to the actual schema.

If you are running this the first time on an existing database, please make sure to read this documentation page:
https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/troubleshooting-development

[+] Added tables
  - collections

? We need to reset the MySQL database "test" at "127.0.0.1:3306"

Which is exactly the same db as the one I’ve pulled data from.

Expected behavior

Migrate without having to reset data

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model collections {
  id         Int      @id @default(autoincrement())
  title      String   @db.VarChar(255)
  created_at DateTime @db.Date
  slug       String   @unique @db.VarChar(255)

  @@index([slug], map: "collection_slug")
}

Environment & setup

  • OS: Mac Os
  • Database: Mysql 8.0.22
  • Node.js version: v16.13.0

Prisma Version

prisma                  : 3.9.0
@prisma/client          : 3.9.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at ../../.npm/_npx/2778af9cee32ff87/node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : bcc2ff906db47790ee902e7bbc76d7ffb1893009
Studio                  : 0.457.0

Issue Analytics

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

github_iconTop GitHub Comments

12reactions
pantharshit00commented, Feb 1, 2022

Oh yeah sorry. I forgot that --create-only won’t render the migration before it asks for reset. You can do the following instead.

mkdir -p prisma/migrations/init
npx prisma migrate diff --preview-feature --from-empty --to-schema-datamodel prisma/schema.prisma --script > prisma/migrations/init/migration.sql
npx prisma migrate resolve --applied init
npx prisma migrate dev # it should say everything is in sync

You should also add a lock file manually here since you will creating the migrations folder by hand in prisma/migrations/migration_lock.toml

# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "mysql"
3reactions
pantharshit00commented, Feb 1, 2022

No, migrate diff was actually released today. Good timing 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrations - Drift - Simon Binder
Drift provides a migration API that can be used to gradually apply schema changes after bumping the schemaVersion getter inside the Database class....
Read more >
Migration troubleshooting in development - Prisma
Database schema drift occurs when your database schema is out of sync with your migration history - the database schema has 'drifted away'...
Read more >
Detecting Database Drift during Flyway Database Development
You can try migrating your pubs database to a version, altering the database manually (for example, just open SSMS and create a couple...
Read more >
Detect and resolve drift in AWS Control Tower
Resolving drift · Many types of drift can be resolved through the Landing zone settings page. · If your OU has fewer than...
Read more >
Tutorial: Using Flyway Check with SQL Server
-drift produces a report showing objects in the schema which are not the result of any of the currently applied migrations, i.e. changes...
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