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.

postgres: Inappropiate migration generated for 'default: null'

See original GitHub issue

Issue type:

[x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.28

Steps to reproduce or a small repository showing the problem:

This issue appears very similar to https://github.com/typeorm/typeorm/issues/5509 but for postgres.

Minimal entity declaration:

import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Foo {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  requiredString: string;

  @Column({ type: "text", nullable: true })
  optionalString: string | null;

  @Column({ type: "text", nullable: true, default: null })
  optionalStringWithExplicitDefault: string | null;
}

Even after syncing the schema, generating a migration still results in the lines

// from `up` method
await queryRunner.query(`ALTER TABLE "foo" ALTER COLUMN "optionalstringwithexplicitdefault" SET DEFAULT null`);

// in `down` method
await queryRunner.query(`ALTER TABLE "foo" ALTER COLUMN "optionalstringwithexplicitdefault" DROP DEFAULT`);

Expected behavior: the migration should not include these lines.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
vegerotcommented, Oct 30, 2020

@brahn I have the same issue. Looking at https://github.com/typeorm/typeorm/issues/150#issuecomment-272158658 , it appears as a temporary workaround we can do

@Column({ type: "text", nullable: true, default: () => "NULL" })

Let me know if this works for you

0reactions
vegerotcommented, Jan 12, 2021

To be more specific, when you generate multiple new migrations, each time the case of null gets flipped.

For example, at first it will say

query: ALTER TABLE "air_refuels" ALTER COLUMN "missionId" SET DEFAULT null

then after I run that migration, and gen a new one, the next migration will say

query: ALTER TABLE "air_refuels" ALTER COLUMN "missionId" SET DEFAULT NULL

After you run the next migration, generating yet another migration produces

query: ALTER TABLE "air_refuels" ALTER COLUMN "missionId" SET DEFAULT null

I don’t think @brahn noticed this when writing the issue.

This certainly seems like a TypeORM bug. Please let me know what I can do to help resolve it

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare a nullable enum by default null in postgreSQL ...
I'm new on Knex, and I have to resolve a migration that adds a column with a value of type enum which should...
Read more >
Documentation: 15: ALTER TABLE - PostgreSQL
The new default value will only apply in subsequent INSERT or UPDATE commands; it does not cause rows already in the table to...
Read more >
Avoid SET/DROP DEFAULT unless a field changes from null ...
Migration forces "DROP DEFAULT" SQL → Avoid SET/DROP DEFAULT on field alteration if not required. Triage Stage: Unreviewed → Accepted. Type: ...
Read more >
Troubleshooting migration tasks in AWS Database Migration ...
The error "Relation 'awsdms_apply_exceptions' already exists" often occurs when a Redshift endpoint is specified as a PostgreSQL endpoint. To fix this issue, ...
Read more >
The Complete Oracle to Postgres Migration Guide - EDB
Learn everything about migrating from Oracle to Postgres, ... GENERATED BY DEFAULT allows the user the option to insert or update a value ......
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