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

prisma migrate dev will not allow for db level default on scalar list #11379

Closed
Tracked by #13318
nicklloyd opened this issue Jan 25, 2022 · 10 comments
Closed
Tracked by #13318

prisma migrate dev will not allow for db level default on scalar list #11379

nicklloyd opened this issue Jan 25, 2022 · 10 comments
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/psl-wg team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: default topic: prisma gaps topic: scalar list []
Milestone

Comments

@nicklloyd
Copy link

Bug description

Similar to #8330, we have a field that we want to store in our Postgres db as a scalar list.

As stated here:

To work around this issue, you can set the default value of array fields to {} at a database level

When we set the default in pg however, any subsequent run drops the default.

How to reproduce

  1. Declare field as an array.
model Lead {
  requestedServices String[]
}
  1. Ensure db and schema are aligned
prisma migrate dev
  1. Set default field value in pg
ALTER TABLE "Lead" ALTER COLUMN "requestedServices" SET DEFAULT '{}';

  1. Run migrate again
prisma migrate dev

CLI Output:

Datasource "db": PostgreSQL database "prisma_dev", schema "public" at "localhost:5432"

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.

[*] Changed the `Lead` table
  [*] Altered column `requestedServices` (default changed from `None` to `Some(DbGenerated("'{}'::text[]"))`)

? We need to reset the PostgreSQL database "prisma_dev" at "localhost:5432".
Do you want to continue? All data will be lost. › (y/N)

Answering y to the prompt then drops data and default on field.
Answering N cancels migration and does not affect db, but prompt returns on next run.

As an aside, if the steps above are repeated, except a new migration is generated for altering a different table/field, the new migration file is generated, but contains only intended changes:

-- AlterTable
ALTER TABLE "PartnerRequest" ALTER COLUMN "status" SET DEFAULT E'UNCONFIRMED';

However the default on the other field is also dropped.

Expected behavior

If prisma cannot support setting a DEFAULT on a scalar list (pity...), I should at least be able to set one on a db level and not have it reverted or cause issues with continuing workflow.

Prisma information

model Lead {
  id                  String      @id @unique @default(cuid()) @db.VarChar(30)
  name                String?     @db.Text
  firstName           String?     @db.Text
  lastName            String?     @db.Text
  email               String?     @db.Text
  telephone           String?     @db.Text
  role                String?     @db.Text
  companyName         String?     @db.Text
  website             String?     @db.Text
  notes               String?     @db.Text
  referer             String?     @db.Text
  srcUrl              String?     @db.Text
  // add default '{}' when prisma is ready to support
  requestedServices   String[]
  createdAt           DateTime    @default(now()) @db.Timestamp
  updatedAt           DateTime    @default(now()) @db.Timestamp
}

Environment & setup

  • OS: Mac 12.1 (21C52)
  • Database: postgres:14.1-alpine
  • Node.js version: v14.17.6

Prisma Version

prisma                  : 3.8.1
@prisma/client          : 3.8.1
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio                  : 0.452.0
@nicklloyd nicklloyd added the kind/bug A reported bug. label Jan 25, 2022
@nicklloyd
Copy link
Author

@janpio: hoping the above helps get this one some attention

@janpio janpio added the team/schema Issue for team Schema. label Jan 25, 2022
@janpio
Copy link
Member

janpio commented Jan 25, 2022

Super wild idea: Can you try adding a @default(dbgenerated("{}")) to your schema for that field to see if this makes a difference?

@nicklloyd
Copy link
Author

Any attempt to set default on list is not happy...

image

Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "prisma_dev", schema "public" at "localhost:5432"

Error: Schema parsing
error: Error parsing attribute "@default": Cannot set a default value on list field.
  -->  schema.prisma:123
   | 
122 |   installationRequest Boolean?    @default(false)
123 |   requestedServices   String[]    @default(dbgenerated("{}"))
   | 

@floelhoeffel floelhoeffel added the bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. label Jan 27, 2022
@do4gr do4gr self-assigned this Feb 25, 2022
@Jolg42 Jolg42 added this to the 3.12.0 milestone Mar 16, 2022
@janpio janpio added the tech/engines Issue for tech Engines. label Mar 24, 2022
@flesler
Copy link

flesler commented Apr 27, 2022

This would be a nice change and doesn't seem huge. Just allow the current syntax to be used without an artificial limitation
@default(dbgenerated("{}")) 🙏

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Apr 27, 2022
@janpio
Copy link
Member

janpio commented Apr 27, 2022

Is DEFAULT {} the correct PostgreSQL syntax to define an empty list as default?

@flesler
Copy link

flesler commented Apr 27, 2022

No, rather DEFAULT '{}' with apostrophes. It automatically casts to the real type.
For example: String[] -> DEFAULT '{}'::text[]

@janpio
Copy link
Member

janpio commented Apr 27, 2022

Then the dbgenerated value probably would need to look like this:

@default(dbgenerated("'{}'"))

With this is should generate proper SQL then.

Buuuut, I know we are actually planning to add proper @default([]) (or @default([1, 2]) with values) support soon, which should remove the artificial limitation at the same time.

@flesler
Copy link

flesler commented Apr 27, 2022

Buuuut, I know we are actually planning to add proper @default([]) (or @default([1, 2]) with values) support soon, which should remove the artificial limitation at the same time.

That would be way better. Was just trying to get the fastest solution out even if not ideal

@pimeys pimeys assigned tomhoule and unassigned do4gr Jun 1, 2022
@EvanDarwin
Copy link

Buuuut, I know we are actually planning to add proper @default([]) (or @default([1, 2]) with values) support soon, which should remove the artificial limitation at the same time.

Wonderful! This issue is a bit of a blocker for me as well, so I've been watching this issue closely.

@janpio janpio modified the milestones: 3.12.0, 4.0.x Jun 7, 2022
@janpio
Copy link
Member

janpio commented Jun 11, 2022

This will soon be solved via #8330

@janpio janpio closed this as completed Jun 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/psl-wg team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: default topic: prisma gaps topic: scalar list []
Projects
None yet
Development

No branches or pull requests

8 participants