Skip to content
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

Ability to drop foreign key #157

Closed
fjorgemota opened this issue Jan 16, 2014 · 8 comments
Closed

Ability to drop foreign key #157

fjorgemota opened this issue Jan 16, 2014 · 8 comments

Comments

@fjorgemota
Copy link

Hello.

Thanks for the great library!

I am testing Knex in a NodeJS application and found a thing that makes sense for migrations: The hability to drop foreign keys in Schema, without dropping columns.

This make senses to allow a field to be renamed correctly when having a foreign key to another table, for example.

And, if possible, too, allow the renameColumn function to detect correctly when a field is a foreign key, drop it and re-create it after the renaming.

But...these is just suggestions, the community is free to think about.

Thanks and sorry for the poor english.

@bendrucker
Copy link
Member

Are you asking for the ability to rename columns acting as foreign keys? The DB is going to complain.

@fjorgemota
Copy link
Author

@bendrucker For some reason Knex does not sends the definition of the field (e.g Type, if it is null or not and etc.) when renaming columns, this is necessary to MySQL.

About Foreign Keys, adding the types correctly simply does not work, MySQL emits a error about "Foreign Key constraint is incorrectly formed". In time: See this question at stackoverflow about this subject

Thanks for the answer, anyway. =)

@bendrucker
Copy link
Member

There's an existing issue open that covers this #46

Definitely on the radar

@nikku
Copy link

nikku commented Jul 3, 2014

Droping foreign keys is possible in version >= 0.6.x:

Suppose you added a foreign key column via

knex.schema.table('me', function(t) {
  t.integer('other_id')
      .unsigned()
      .references('other.id')
      .onDelete('cascade');
})

you are able to drop it again via

knex.schema.table('me', function(t) {
  t.dropForeign('other_id');
  t.dropColumn('other_id');
});

Just make sure you dropped the foreign key before or with the actual column.

@stringbeans
Copy link

Can the maintainer please add this to the documentation?

@rhys-vdw
Copy link
Member

Hi @stringbeans. There is currently an open issue tracking these missing schema docs #483. Please provide a PR containing the missing method documentation and I'll gladly merge them into our gh-pages branch. Thanks!

@stringbeans
Copy link

@rhys-vdw thanks! and apologies for not searching for the other issue!

@rhys-vdw
Copy link
Member

@stringbeans dropForeign and foreign docs have just been added via #1156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants