b.herring
b.herring

Reputation: 653

Relational databases and foreign keys

I just have a couple of questions about references, foreign keys and associations in rails. Ive read documentation but still a bit confused.

  1. When you type t.references :tweets for example in a migration, why does this then turn into bigint in the schema?
  2. Is foreign key :true the part of the code that links the two tables?
  3. In addition to question 2, what role does 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

Answers (1)

tadman
tadman

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

Related Questions