migration ":dependent" option
in migration file, what is ":dependent
" option used for, what could be the possible values for this option? And what does each value mean? Can anybody explain to me?
e.g. here it uses :dependent => :delete
def self.up
create_table :car, :id => false do |t|
t.integer :brand_id, :null =>false
t.timestamp :buy_at, :null开发者_如何学Go => false
end
add_index :car, :brand_id
add_foreign_key :car, :brands, :dependent => :delete
end
def self.down
drop_table :balance_updated
end
In "normal" Rails, the :dependent
option is used in the model like so:
has_many :models, :dependent => :destroy
However, I think you are using the foreign_key_migrations plugin?
If so, take a look at this source code line to know what the :dependent
option is doing.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. Reference
So the car's column brand_id is depending on the (primary key) of the brand's table typically going to be called brand_id (default naming in sqlite3, if your using the default rails database).
So this ensures that if the brand is deleted i.e Mercedes from the Brand table then ALL cars that are branded as Mercedes in the Car table are also deleted.
精彩评论