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

Allow setting scalar list default values #8330

Closed
Tracked by #13318
casey-chow opened this issue Jul 17, 2021 · 7 comments · Fixed by prisma/prisma-engines#2946
Closed
Tracked by #13318

Allow setting scalar list default values #8330

casey-chow opened this issue Jul 17, 2021 · 7 comments · Fixed by prisma/prisma-engines#2946
Assignees
Milestone

Comments

@casey-chow
Copy link

casey-chow commented Jul 17, 2021

Problem

I'm currently migrating a schema which has a non-empty string default. My schema looks like this:

model Person {
  emails String[]
}

This is how it is generated from prisma introspect, decided from #1110. However, when running prisma migrate dev, the generated migration then attempts to remove the default, generating:

ALTER TABLE "Person" ALTER COLUMN "emails" DROP DEFAULT;

Removing this and attempting to add a default using dbgenerated leads to the following error:

Error parsing attribute "@default": Cannot set a default value on list field.

Suggested solution

As far as I can tell, this error is entirely artificial at this point because the release of 2.16.0 introduced a workable syntax for scalar lists via dbgenerated. I'd like to propose simply removing this check:

https://github.com/prisma/prisma-engines/blob/215319b1f112e9e5b5a1f2d2b0553982d2669776/libs/datamodel/core/src/transform/ast_to_dml/db/attributes.rs#L320-L322

Alternatives

None considered, as I haven't found any drawbacks to this proposal.

Additional context

@pantharshit00
Copy link
Contributor

Thanks for bringing this up, I agree we should finally decide on how we can support defaults for scalar lists.

@pantharshit00
Copy link
Contributor

pantharshit00 commented Jul 18, 2021

Can you also try adding @default(dbgenerated()) which might prevent the faulty migration? (We might be actually blocking this no harm in trying)

model Person {
  emails String[] @default(dbgenerated())
}

@pantharshit00 pantharshit00 added kind/feature A request for a new feature. team/schema Issue for team Schema. topic: postgres list labels Jul 18, 2021
@casey-chow
Copy link
Author

Can you also try adding @default(dbgenerated()) which might prevent the faulty migration? (We might be actually blocking this no harm in trying)

model Person {
  emails String[] @default(dbgenerated())
}

No dice, same error 😞

@nicklloyd
Copy link

This one is a bit painful...

We're running:

version "3.8.1"

Updating migration.sql to include the default doesn't help either are seems like any prisma migrate run also generates a new migration to drop the default...

Any other work arounds?

@nicklloyd
Copy link

Last sentence https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-scalar-lists-arrays#null-values-in-arrays states:

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

But have been unable to use migrate without it reseting, even if I alter the table in PG itself

@janpio
Copy link
Member

janpio commented Jan 24, 2022

Open an issue about migrations not picking that up correct please and provide all the information the issue template asks for @nicklloyd . If that is the case, it is not intended.

@janpio
Copy link
Member

janpio commented Feb 25, 2022

Internal note:
There are some more similar issues collected in this column in a project board: https://github.com/orgs/prisma/projects/64#column-17788443

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

Successfully merging a pull request may close this issue.

8 participants