db-migrate / pg

A postgresql driver for db-migrate.
Other
67 stars 51 forks source link

addForeignKey Error -->Cannot read property 'onDelete' of undefined #37

Closed ndouglasma closed 1 year ago

ndouglasma commented 6 years ago

I'm submitting a...

Current behavior

I'm creating my first table through db-migrate (YEAH) and added a foreign key to one of the columns:

exports.up = function(db, callback) { db.createTable('code_values', { columns: { id: { type: "int", primaryKey: true, notNull: true, autoIncrement: true }, category_id: { type: "int", notNull: true, foreignKey: { name: "code_values_code_category_id_fk", table: "code_categories", mapping: { category_id: "id" } } } } }, callback); };

I get the following error when I run the migration:

[ERROR] TypeError: Cannot read property 'onDelete' of undefined at Object.addForeignKey (/test/node_modules/db-migrate-pg/index.js:542:13) at /test/node_modules/db-migrate-base/index.js:124:21 at Object.recurseCallbackArray (/test/node_modules/db-migrate-base/index.js:104:37) at Object.<anonymous> (/test/node_modules/db-migrate-base/index.js:243:21) From previous event: at Object.createTable (/test/node_modules/db-migrate-base/index.js:241:6) at /usr/local/lib/node_modules/db-migrate/lib/driver/shadow.js:34:50 at runCallback (timers.js:763:18) at tryOnImmediate (timers.js:734:5) at processImmediate (timers.js:716:5)

Expected behavior

I expected the table to be created without the explicit rules added to the foreign key. As for the foreign key, I expected it to default to NO ACTION for ON DELETE and ON UPDATE.

What is the motivation / use case for changing the behavior?

I was able to get things working when adding the following rules to the foreign key:

rules: { onDelete: 'RESTRICT', onUpdate: 'RESTRICT' }

Looking at db-migrate-pg's index.js, I believe the problem is that the function addForeignKey mandates a value for rules; and subsequently when rules.onDelete is called, the variable is undefined.

I'm willing to check this out further and work on a fix, but would love feedback in advance, especially since this would be my first time contributing to an open-source project. Please let me know your thoughts and understand, I'd like to isolate my first potential commit to this specific item (updated documentation included of course) and look globally in the future.

Environment


db-migrate version: 0.11.1
db-migrate-pg version: 0.4.0

Additional information:
- Node version: 9.9.0
- Platform:  Mac
- PostgreSQL 10.3
- Postgres 2.1.5



---
Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/62575565-addforeignkey-error-cannot-read-property-ondelete-of-undefined?utm_campaign=plugin&utm_content=tracker%2F14258523&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F14258523&utm_medium=issues&utm_source=github).
            
DanceParty commented 3 years ago

Yes, I think this can be solved by opening a PR and replacing the lines from:

rules.onDelete || 'NO ACTION',
rules.onUpdate || 'NO ACTION'

to

(rules && rules.onDelete) || 'NO ACTION',
(rules && rules.onUpdate) || 'NO ACTION'

In the meantime, you should just pass an empty object to the rules property:

    person_id: {
      type: "string",
      foreignKey: {
        name: "this_table_person_id_fk", 
        table: "friend", 
        mapping: "id",
        rules: {}
      }
    },