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.

Generated columns (PostgreSQL)

See original GitHub issue

Problem

Postgres generated columns are currently not supported. That’s not a big deal, since we can always declare them as regular columns in the schema.prisma. The only flaw is we need to declare them as optional, otherwise the client will always complain about them missing in the create input. This means the generated type field will always be “nullable”, when it’s actually not.

Suggested solution

Introduce a custom schema attribute ( ex @generated ) to mark a column as a postgres generated column. This way it will not be included in the create input type but it will also be not null in the model type.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:28
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
andyjycommented, Aug 15, 2022

Update: issue created https://github.com/prisma/prisma/issues/14799


I’m seeing a related issue which is that prisma inspects the database schema for a generated column I want it to not attempt to manage and writes into any new migrations ALTER TABLE <> ALTER COLUMN <generated_thing> DROP DEFAULT;

@ryanmt I’ve just run into this after upgrading to Prisma 4 - these unwanted (+invalid!) DROP DEFAULT migrations were not being generated previously, so now I’ve hit a blocker preventing me creating new migrations.

Are you using Prisma v4 also? Could you possibly share more about your workaround?

~~

In my schema.prisma (worked for the past months with Prisma v3):

...
<column> String? @db.VarChar(255) @default(dbgenerated())
...

A previous custom migration in my migrations history results in the following schema in Postgres (which has been working fine):

"<column>" character varying(255) GENERATED ALWAYS AS (
<SQL expression>) STORED

Upgrading to Prisma v4 now generates these new DROP DEFAULT migrations that fail with error:

Database error:
ERROR: column "<column>" of relation "<table>" is a generated column
HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.
6reactions
pantharshit00commented, Jul 21, 2021

I think you can already use @default(dbgenerated()) here to specify that field is computed. We already support that. https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#dbgenerated

What we don’t do here is automatically add this for computed fields when we introspect so that is something that we should do. cc @do4gr

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 5.3. Generated Columns - PostgreSQL
A generated column is a special column that is always computed from other columns. Thus, it is for columns what a view is...
Read more >
New In PostgreSQL 12: Generated Columns - pgDash
PostgreSQL 12 comes with a new feature called generated columns. Other popular RDBMSes already support generated columns as “computed columns” or “virtual ...
Read more >
PostgreSQL Generated Always: Generate Computed Columns
A generated column is a unique column that is always computed from other existing columns. You cannot insert data into generated column but...
Read more >
How to Create a Generated Column in PostgreSQL
In PostgreSQL, a generated column is a special column that is always computed from other columns. A generated column doesn't have a fixed ......
Read more >
An Overview of Generated Columns for PostgreSQL
A Generated Column is a special column in a table that contains data automatically generated from other data within the row. The content...
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