-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
create_join_table should include indexes and foreign key contraints #22960
Comments
I agree! I have to do this all the time. Maybe we could add options to the generator, like: |
I don't know, the migrations mostly don't use options like that. You can already enable the index by using
Produces
And you can use references to create an index as well when creating models. Like so:
Which produces:
So maybe it would be best to allow something like:
Creating this:
And then the default
|
Fixes issue rails#22960 When creating join tables with the command rails g migration CreateJoinTableShowroomUser showroom:references user:references The migration will use references to create the joins and output: class CreateJoinTableShowroomUser < ActiveRecord::Migration def change create_join_table :showrooms, :users do |t| t.references :showroom, index: true, foreign_key: true t.references :user, index: true, foreign_key: true end end end This allows for proper refrences with indexes and foreign keys to be easily used when adding join tables. Without `:refrences` the normal output is generated: class CreateJoinTableShowroomUser < ActiveRecord::Migration[5.0] def change create_join_table :showrooms, :users do |t| # t.index [:showroom_id, :user_id] # t.index [:user_id, :showroom_id] end end end
+1 @erullmann This is a very nice solution. 😃 |
Currently, if one generates a join table like this:
the following migration will be created:
Running this migration directly will create a very simple join table:
So no indexes, no foreign keys etc. If one uncomments the two lines in the migration, now one gets:
I would argue, that the generated migration should look like this:
If one uncomments the two
references
lines, the produced table will look like this:Why wouldn't somebody want foreign keys and indexes on them in a join table? @rafaelfranca
The text was updated successfully, but these errors were encountered: