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.

Unclear error message when using prisma-level default functions (cuid, uuid) as default for non-nullable fields

See original GitHub issue

Bug description

After adding a NON NULLABLE column with a default value to an existing table with records in the database, I get the following error:

Added the required column `x` to the `y` table without a default value. There are `z` rows in this table, it is not possible to execute this migration.

The model looks like the following:

model MyModel {
    id   String   @default(cuid())             @id
    x    String   @default("Some value")
    // And some other columns...
}

The column causing the error is the x column.

How to reproduce

  1. Create a migration with a model and some columns.
  2. Add a NON NULLABLE column to that model.
  3. Save the changes and run the migration.
  4. See error.

Expected behavior

Migration to be successfully applied.

Environment & setup

  • OS: Windows 10 2004 (Running on WSL 2)
  • Database: PostgreSQL
  • Node.js version: 12.18.2
  • Prisma version: 2.1.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

11reactions
tomhoulecommented, Sep 11, 2020

cuid() and uuid() are not database-level defaults — they are instead populated by the prisma-client. The only workaround in this case is to add the columns as optional, populate them, then make them required. I’ll keep this issue open as a reminder to expand the error message.

5reactions
Madmeerkat-creatorcommented, Jan 28, 2021

Hi! I had a same problem, having error <Step 1 Added the required column password to the User table without a default value. There are 1 rows in this table, it is not possible to execute this migration.> So I checked my MySQL Workbench and I found that there is a data. So I erased it… and it finally worked! Here is my code

model User {
  id      Int       @default(autoincrement()) @id
  email   String    @unique
  name    String
  password  String
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

prisma nullable column | The Search Engine You Control
prisma/prismaUnclear error message when using prisma-level default functions (cuid, uuid) as default for non-nullable fields#4691. Created over 2 years ago.
Read more >
Prisma 2.19.0 Release - GitClear
Better error when adding a new required field and the default value is Prisma-level, e.g. uuid(); Small improvement where Prisma Migrate failed ...
Read more >
MySQL 8.0.13: Default Value as uuid not working
This is unfortunately a bug with default expressions for primary key columns, Expression Default is made NULL during CREATE TABLE query, ...
Read more >
Default value for UUID column in Postgres
tl;dr. Call DEFAULT when defining a column to invoke one of the OSSP uuid functions. The Postgres server will automatically invoke the ...
Read more >
@prisma/cli: Versions | Openbase
... when using prisma migrate dev · Unclear error message when using prisma-level default functions (cuid, uuid) as default for non-nullable fields ......
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