rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Rails/CreateTableWithTimestamps shouldn't warn when id: false is used

Open connorshea opened this issue 6 years ago • 2 comments

In migrations you can create a join table like so:

create_table :games, :genres, id: false do |t|
  t.integer :game_id
  t.integer :genre_id
end

But Rails/CreateTableWithTimestamps will warn about this code because the table is being created without timestamps. Join tables shouldn't have timestamps, so this is a false positive.

https://guides.rubyonrails.org/association_basics.html#creating-join-tables-for-has-and-belongs-to-many-associations

connorshea avatar Jan 03 '19 01:01 connorshea

I think it's still useful to have timestamps on a join table, even if just for troubleshooting.

andyw8 avatar Jan 03 '19 01:01 andyw8

I disagree with @andyw8, because it's seems useless for habtm tables. It useful for more complex join tables. Simple join/habtm tables easily can increase time of response and overflood your db with unusable two magic time fields (think about that join record will be touch updated with updated_at when associated records will also update)

My opinion is to have only id's fields and when you really need timestamps - add them(during my 15y Rails career never had to use timestamps in simple habtm's)

Therefore I totally agree with @connorshea that this is false positive

alexey avatar Sep 10 '20 09:09 alexey

Thank you Koichi!

connorshea avatar Apr 05 '23 19:04 connorshea