Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A Model for view MySQL in Prisma Schema will generating a new table with the same name once running Migrate command #6015

Closed
dvlwj opened this issue Mar 8, 2021 · 7 comments
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: migrate

Comments

@dvlwj
Copy link

dvlwj commented Mar 8, 2021

Bug description

When we are creating a model in Prisma Schema (using this documentation) to read from the view table in MySQL everything work fine.

But, when we running migrate command: npx prisma migrate dev --preview-feature, it's will create a new "data" table with the same name in MySQL Database.

This is because when we running the command in the CLI, in the end, it's will create a new migration file with CREATE TABLE function instead of CREATE VIEW function.

How to reproduce

Steps to reproduce the behavior:

  1. Create a new DB or use the existing DB
  2. Create a new view (a simple one can work) or use the existing view in MySQL
  3. Create the schema for this view table in Prisma Schema (schema.prisma). Mine is like the script below:
model ViewHours {
  id  Int @id
  open_hour Int @default(9)
  close_hour Int @default(17)
  active Boolean? @default(true)

  @@unique([id], name: "id")
  @@map("v_hours")
}
  1. Run npx prisma migrate dev --preview-feature in your app's root directory
  2. Follow the steps provide by Prisma CLI.
  3. Check migration file (mine is in app's root directory > prisma > migrations > migration_name > migration.sql)
  4. The supposed to be View is becoming normal table, my example becoming like the query below:
CREATE TABLE `v_hours` (
    `id` INTEGER NOT NULL,
    `open_hour` INTEGER NOT NULL DEFAULT 9,
    `close_hour` INTEGER NOT NULL DEFAULT 17,
    `active` BOOLEAN DEFAULT true,
UNIQUE INDEX `id`(`id`),
INDEX `customer_id`(`id`, `customer_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

which is supposed to be like:

CREATE OR REPLACE VIEW v_hours AS
SELECT
    foo.id,
    bar.open_hour,
    bar.close_hour,
    foo.active
FROM fooTable foo
INNER JOIN barTable bar on foo.id = bar.foo_id;

Expected behavior

Should generate a create or update View query instead create new Table query

Prisma information

I think no need for this because of my explanation above?

Environment & setup

  • OS: Mac OS Big Sur Version 11.2.2 (20D80)
  • Database: MySQL 5.7.23
  • Node.js version: 12.13.1
  • Prisma version:
prisma               : 2.17.0
@prisma/client       : 2.17.0
Current platform     : darwin
Query Engine         : query-engine 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 3c463ebd78b1d21d8fdacdd27899e280cf686223 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.353.0
@dvlwj
Copy link
Author

dvlwj commented Mar 8, 2021

If there is any information missing that Prisma's developer team need, please let me know.

@Jolg42 Jolg42 added kind/feature A request for a new feature. process/candidate team/schema Issue for team Schema. topic: migrate labels Mar 9, 2021
@Jolg42
Copy link
Member

Jolg42 commented Mar 11, 2021

The solution here would be to have views supported in the schema I think, see tracking issue #678 and upvote.

@tomhoule tomhoule added this to the 2.20.0 milestone Mar 17, 2021
@tomhoule
Copy link
Contributor

Assigning myself to create a docs issue.

@dvlwj
Copy link
Author

dvlwj commented Mar 19, 2021

Thanks a lot for informing @Jolg42 , and thanks too to you @tomhoule .

@tomhoule tomhoule modified the milestones: 2.20.0, 2.21.0 Mar 31, 2021
@tomhoule
Copy link
Contributor

Keeping in this cycle to confirm that the docs are fixed.

@tomhoule
Copy link
Contributor

tomhoule commented Jul 2, 2021

This problem has been documented here: prisma/docs#1756

@tomhoule tomhoule removed their assignment Jul 2, 2021
@tomhoule tomhoule removed this from the 2.27.0 milestone Jul 2, 2021
@dvlwj
Copy link
Author

dvlwj commented Jul 2, 2021

Hi @tomhoule ,

I will close the issues then.

Thanks a lot.

@dvlwj dvlwj closed this as completed Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: migrate
Projects
None yet
Development

No branches or pull requests

4 participants