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.

addColumn in migration does not work as expected

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

[ ] latest [ ] @next [x] 0.2.8

I have a migration to add a new column to an existing table (with data in it) using the following code:

async up(queryRunner: QueryRunner): Promise<any> {
    await queryRunner.addColumn('myTable', new TableColumn({
        name: 'my_new_column_name',
        type: 'varchar',
        isNullable: true
    });
}

My expectation was that this code would be transformed to an ALTER TABLE query in the background. When logging all database activity, I saw it actually tried to create a temporary table and drop the actual table. This seems weird to me and it also gave me an error because there was already a foreign key constraint on another column in this table. So the migration didn’t work. I fixed it by manually putting in an ALTER TABLE query, but shouldn’t addColumn already do that?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

2reactions
andrewdhazlettcommented, Jan 27, 2020

I was able to fix a similar issue (on postgres) by wrapping my default value in single quotes: image

Previously I was getting the error column "open" does not exist: image

This is downright baffling behavior.

2reactions
WimWidgetscommented, Apr 23, 2019

@lili21 As I said in the last line: I fixed it by manually putting in an ALTER TABLE query, […]

So not a real structural fix, but at least I got it working.

await queryRunner.query(`ALTER TABLE "myTable" ADD COLUMN "my_new_column_name" VARCHAR(255) DEFAULT NULL`);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity framework migration - add new column with a value for ...
Add code like this after the AddColumn command, so that the column is already available in the table when the SQL statement runs:...
Read more >
Auto Migration failing when just adding 2 new columns to ...
I can only assume that this is a bug in the auto migration logic, perhaps the new columns are being cached as keys,...
Read more >
Can't add column to existing table - Laracasts
I wanna add a column to an existing table, but it always says Nothing to migrate. What I did so far I added...
Read more >
PostgreSQL: ALTER TABLE ... ADD COLUMN … done right
Running ALTER TABLE … ADD COLUMN can cause serious issues in production. Understand how to cleanly change PostgreSQL data structures.
Read more >
Documentation: 15: ALTER TABLE - PostgreSQL
When multiple subcommands are given, the lock acquired will be the strictest one required by any subcommand. ADD COLUMN [ IF NOT EXISTS...
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