New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for custom foreign key names #1311
Added support for custom foreign key names #1311
Conversation
@@ -66,7 +66,7 @@ TableCompiler.prototype.alter = function () { | |||
|
|||
TableCompiler.prototype.foreign = function (foreignData) { | |||
if (foreignData.inTable && foreignData.references) { | |||
var keyName = this._indexCommand('foreign', this.tableNameRaw, foreignData.column); | |||
var keyName = foreignData.keyName || this._indexCommand('foreign', this.tableNameRaw, foreignData.column); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I wrap foreignData.keyName with this.formatter.wrap()
?
If you think this feature is likely to be accepted, I'll be happy to update the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, .wrap
should be called on keyName as well.
@natorojr This looks good, and thanks for doing this. Interesting that
|
19c4622
to
b146316
Compare
Thanks for the feedback.
I removed build/knex.js from the PR
In other words, it allows supporting both of the following:
I added a note about However, the current documentation suggests that Do you have any idea why the documentation for
I'll gladly write some unit tests, but don't want to waste time if this is an incompatible change with supprt for multiple columns (see note above). |
@natorojr Yes, it does accept multiple columns. I'm not too sure about most dialects, but here's a page with Postgres references and examples. The output of using multiple columns is the following: let qBuilder = knex.schema.createTable('foo', function(table) {
table.foreign(['bar', 'bar2']).references('id').inTable('bar');
});
console.log(qBuilder.toString());
//create table "foo" ();
//alter table "foo" add constraint foo_bar_bar2_foreign foreign key ("bar", "bar2") references "bar" ("id") Support for multiple columns is only when specifying the first argument as an array, it does not work when supplying several column arguments. Multiple columns or not I believe that's a moot argument in regards to Edit: Which I now see you added. 👍 |
Wow, I learned something new today. Thanks for the Postgres documentation reference. From the Postgres documentation:
Does that imply your example above should be:
? Either way, to your point, it seems like my changes should be compatible. I'll work on some unit tests in the next few days and update the PR. I only have access to Postgres and SQLite on my machine at this time, so I'll start there. I'll try to add MySQL after that. Apologies in advance for delays. |
@vellotis Sorry for the delayed response. I had planned to finish this PR but, as is the case for many contributors, I got tied up with other work efforts/commitments. You're welcome to rebase and add the tests yourself. Otherwise, I'll try to circle back to this when the dust settles. |
* Added support for custom foreign key name * Update unit test for #1311 * Fix TableCompiler::foreign to respect previous behavior + typo * Add dialect specific unit tests * Fixed comment Actually trying to trigger travis to run tests with latest travis configuration... (maybe it just doesn't run tests against merged branch..)
Greetings,
I'm working on a project which requires the ability to set foreign key constraint names that follow a specific naming convention. I noticed that Knex allows its users to set custom index names via
table.index()
andtable.unique()
methods, but not viatable.foreign()
orcolumn.references().inTable()
.This PR adds the ability to set a custom FK constraint name either as a second parameter to
table.foreign()
or with a chainablecolumn.references().inTable().withKeyName()
method.I only have access to a couple of databases (SQLite and PostgreSQL) so I could only test those. Unless I'm overlooking something, the implementations seems to be completely backwards compatible, so it will not hurt existing code in the wild.
Note, I also updated the documentation accordingly.
My project is time sensitive so I'm hoping I could get this reviewed and merged in sooner than later with a new
npm
package (v0.10.1?), but I understand you may have other requirements to meet before merging. I'll be happy to accommodate any feedback into this PR.Thanks,
Nestor