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.

Foreign Key onDelete not a function

See original GitHub issue

Trying on add onDelete('CASCADE') to a parent_id foreign key. I’ve dropped the column with t.dropColumn('parent_id'), and now I’m trying to add the onDelete part. But t.foreign('parent_id').references('id').onDelete('CASCADE') doesn’t work because t.foreign(…).references(…).onDelete is not a function

How can I get this to work? Why can’t I chain onDelete to this foreign key creation?

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
rbadillapcommented, Jun 17, 2017

@elhigu please correct me if I’m wrong, for me this is not a bug due this example is not indicating the table to be related.

On this piece of code:

t.foreign('parent_id').references('id').onDelete('CASCADE');

parent_id is the foreign key (assuming that it was defined before), but which is the table related?

There’s 3 ways to reference a table in a foreign key definition:

  1. Within references
t.foreign('parent_id').references('tableName.id').onDelete('CASCADE');
  1. Using on
t.foreign('parent_id').references('id').on('tableName').onDelete('CASCADE');
  1. Using inTable (which is an alias of on)
t.foreign('parent_id').references('id').inTable('tableName').onDelete('CASCADE');

These 3 ways works perfectly with no issues.

Thirdly and finally, take a look at https://github.com/tgriesser/knex/blob/master/src/schema/tablebuilder.js#L202 there’s a condition to check if references contains a dot (.), if don’t, there’s no return, that’s why you’re getting the error: onDelete is not a function

0reactions
elhigucommented, Jun 20, 2017

@rbadillap Looks like you are right that those work fine, however better error message would be nice. I’m really surprised that .references('tableName.id') and .references('id') returned different builder as a result (+ funnily it works in other order when cascade is given first and references last).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: this.props.onDelete is not a function reactjs
The problem comes from the line onDelete={this.remove}. This remove function is not defined. Is there anything your wnat to do in onDelete?
Read more >
Django database integrity: ForeignKey on_delete options
The PROTECT argument of the ForeignKey on_delete option prevents the referenced object from being deleted if it already has an object ...
Read more >
SQL Server: Foreign Keys with set null on delete
A foreign key with "set null on delete" means that if a record in the parent table is deleted, then the corresponding records...
Read more >
SQLite Foreign Key Support
Foreign key ON DELETE and ON UPDATE clauses are used to configure actions that take place when deleting rows from the parent table...
Read more >
Associations - Sequelize
For example, to use UUID as the foreign key data type instead of the default ... "fooId" INTEGER NOT NULL REFERENCES "foos" ("id")...
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