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.

Cannot addColumn with a primaryKey on Postgres

See original GitHub issue

Hello!

After browsing the issues, I found a similar issue posted a couple of days ago (https://github.com/db-migrate/node-db-migrate/issues/407). However, this one’s different, as it has to do with postgres (psql).

It seems that I cannot add a column while referencing it to be a primary key while using addColumn. To make matters more precise, the error is:

[ERROR] TypeError: Cannot read property 'emitPrimaryKey' of undefined
    at Class.Base.extend.createColumnConstraint (.../node_modules/db-migrate/lib/driver/pg.js:200:39)
    at Class.Base.extend.createColumnDef (.../node_modules/db-migrate/lib/driver/pg.js:29:31)
    at Class.module.exports.Class.extend.addColumn (.../node_modules/db-migrate/lib/driver/base.js:178:20)
    at exports.down (.../migrations/20160829085748-myMigration.js:16:8)
    at Migration._down (.../node_modules/db-migrate/lib/migration.js:176:34)

And the trace goes on. However, I debugged it a bit, and found the error to be the non-passing of objects, therefore resulting on the undefined. Step by step:

My migration:

db.addColumn('table_name', 'column_name', { type: type.STRING, primaryKey: true }, callback);

Where the error occurs: https://github.com/db-migrate/node-db-migrate/blob/bd942bca54fb1d85592e840999529449ac12f055/lib/driver/pg.js#L197

It would seem options are not passed here: https://github.com/db-migrate/node-db-migrate/blob/bd942bca54fb1d85592e840999529449ac12f055/lib/driver/base.js#L178

Therefore resulting on the undefined.

Keep up the good work, and cheers!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wzrdtalescommented, Sep 14, 2016

I will c if I will backport this to the v0.9.x track. As long as 0.10.x is not released on the main track 0.9.x is still supported though.

0reactions
wzrdtalescommented, Sep 28, 2016

Was backported and released under the latest 0.9.x track.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add primary key to PostgreSQL table only if it does not exist
No, there is no IF NOT EXISTS option for the ALTER TABLE statement. – a_horse_with_no_name. Mar 28, 2012 at 11:59.
Read more >
Documentation: 15: ALTER TABLE - PostgreSQL
This form adds a new PRIMARY KEY or UNIQUE constraint to a table based on an existing unique index. All the columns of...
Read more >
PostgreSQL Primary Key
A primary key is a column or a group of columns used to identify a row uniquely in a table. ... It is...
Read more >
sql — Add primary key to PostgreSQL table only if it does not ...
Solution 3: You can try to DROP it before creating it ( DROP has the IF EXISTS clause): ALTER TABLE mail_app_recipients DROP CONSTRAINT...
Read more >
PostgreSQL: Primary Keys - TechOnTheNet
In PostgreSQL, a primary key is created using either a CREATE TABLE statement or an ALTER TABLE statement. · You use the ALTER...
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