Reputation: 653
I just have a couple of questions about references, foreign keys and associations in rails. Ive read documentation but still a bit confused.
t.references :tweets
for example in a migration, why does this then turn into bigint
in the schema?foreign key :true
the part of the code that links the two tables?belongs to
and has many
have in the models that differs from foreign key true
, as i thought that the schema would be able to tell which table has many
and belongs to
depending on which id is in the other table.Upvotes: 0
Views: 43
Reputation: 211750
Rails switched over to BIGINT
by default in version 5.0 because 32-bit unsigned values can overflow and force your database read-only.
The foreign_key: true
part has the effect of ensuring data integrity between two tables. You can't insert values that don't exist.
belongs_to
and has_many
establish different ends of the same relationship. These cannot necessarily be intuited from the schema itself, and it's often the case that these relationships need to be customized with options.
Upvotes: 2