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

Generators add foreign keys on references #17759

Closed
wants to merge 1 commit into from
Closed

Generators add foreign keys on references #17759

wants to merge 1 commit into from

Conversation

derekprior
Copy link
Contributor

If you run a generator such as:

rails generate model accounts supplier:references

The resulting migration will now add the corresponding foreign key
constraint unless the reference was specified to be polymorphic.

@sgrif
Copy link
Contributor

sgrif commented Nov 25, 2014

Perfect, thanks for working on this. Will merge in the morning.

@senny
Copy link
Member

senny commented Nov 25, 2014

@sgrif I'd hold onto this until 5.0. Arguably references itself could add the foreign key directly. We decided to ship a small implementation first and work towards tighter integration later on.

@derekprior
Copy link
Contributor Author

@senny : that would invalidate any foreign key migrations that were added by people running 4.2 (or Foreigner), forcing people to modify past migrations or schema to upgrade to 5.0.

Is that a breaking change you want to add for 5.0? If so, I'm still not certain why this implementation couldn't ship in 4.2, other than that it increases the surface area of that breaking change I guess?

@senny
Copy link
Member

senny commented Nov 25, 2014

@derekprior we have the possibility to add it with an option foreign_key: true and keep the default false. This won't force people to change the migration.

As for the reason to not ship it with 4.2, we haven't yet discussed how add_foreign_key should be integrated in the rest of the migration DSL. There are people that don't use foreign keys so we need to keep this in mind.

@guilleiguaran
Copy link
Member

Agree with @senny on this

@sgrif
Copy link
Contributor

sgrif commented Nov 25, 2014

I've brought this up a couple of times now and the consensus has been that we felt it was a good idea by default. It sucks to change our mind on this after someone has gone and done the work...

I think rails as a whole should be encouraging referential integrity, and generators are the best way to do that. Perhaps we should add a config option which defaults to true like we do for other cases that not everyone uses (like fixtures).

I do feel very strongly that this should be on by default, however. I spend a lot of time mentoring new developers who are starting with rails. This would have a very positive influence on teaching best practices in this regard, same as generating tests.

I agree a foreign key option on references would be nice but we can add that later, and I don't think it should block this.

Would you guys be comfortable with this in 4.2 if we added a config option to turn it off for the people who specifically don't want FKs?

@sgrif sgrif added this to the 5.0.0 milestone Nov 25, 2014
@guilleiguaran
Copy link
Member

afaik Rails didn't have support for foreign keys in past releases because @dhh wasn't a big fan of it (like many other people).

Now we have support for foreign keys in migration DSL but I'm not sure if everyone is ok with the idea of make it the default when generating references in all new apps.

/cc @dhh

@derekprior
Copy link
Contributor Author

I feel like it's worth pointing out again that this doesn't change the behavior of references or add_reference, only the migration generator.

If the foreign key isn't desired it can be removed, but like indexes they are well-proven to be useful. Encouraging their use is a good thing. Otherwise, what's the feature in rails for?

@rafaelfranca
Copy link
Member

Yeah, agree it is fine to ship this with 4.2 since we are not changing references to create automatic foreign key.

If you run a generator such as:

```
rails generate model accounts supplier:references
```

The resulting migration will now add the corresponding foreign key
constraint unless the reference was specified to be polymorphic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants