外键可以为NULL吗?
Foreign key is allowed to be NULL?
我有一个 table,它必须有一个有效且存在的 case_id,除非存在,否则不应创建记录。我不应该传递 NULL case_id.
看起来像:
# == Schema Information
#
# Table name: medical_intervention_expert_answers
#
# id :integer not null, primary key
# case_id :integer
# problem_id :integer
# medical_intervention_id :integer
# created_at :datetime
# updated_at :datetime
# pti :boolean
#
class MedicalInterventionExpertAnswer < ActiveRecord::Base
belongs_to :case
belongs_to :problem
belongs_to :medical_intervention
def self.create_from_data(case_id, problem_id, medical_intervention_id, is_pti)
answer = self.create(case_id: case_id, problem_id: problem_id, medical_intervention_id: medical_intervention_id,
pti: is_pti)
return answer
end
end
如果我传入一个 null 的 case_id 或传入一个不存在的 case_id,则 'answer' 变量应该是 nil,并且数据库应该是未修改的。
我该怎么做才能拥有这种关系?
通过
在迁移中添加外键约束
add_foreign_key :table, :column
有关详细信息,请参阅 ActiveRecord::Migration guide。
对于添加外键,首先添加 table 的名称(不是模型名称),这是第一个参数,第二个参数是外键的名称 table(不是模型姓名)。
作为第一个参数的 table 应该有一个名称特定的列,如下所示:name_foreign_model_id。
例子:
国外模特
class CreateGrades < ActiveRecord::Migration
def change
create_table :grades do |t|
t.string :name, limit: 20
t.integer :level, limit: 2
t.string :next, limit: 20
t.timestamps null: false
end
end
end
并且,
class CreateGoals < ActiveRecord::Migration
def change
create_table :goals do |t|
t.integer :dimension, limit: 2, null: false
**t.integer :grade_id, null: false**
t.string :description, limit: 1024, null: false
t.timestamps null: false
end
**add_foreign_key :goals, :grades**
end
end
您也可以在单独的迁移中添加外键。
有了这个,不应该接受空外键。
我有一个 table,它必须有一个有效且存在的 case_id,除非存在,否则不应创建记录。我不应该传递 NULL case_id.
看起来像:
# == Schema Information
#
# Table name: medical_intervention_expert_answers
#
# id :integer not null, primary key
# case_id :integer
# problem_id :integer
# medical_intervention_id :integer
# created_at :datetime
# updated_at :datetime
# pti :boolean
#
class MedicalInterventionExpertAnswer < ActiveRecord::Base
belongs_to :case
belongs_to :problem
belongs_to :medical_intervention
def self.create_from_data(case_id, problem_id, medical_intervention_id, is_pti)
answer = self.create(case_id: case_id, problem_id: problem_id, medical_intervention_id: medical_intervention_id,
pti: is_pti)
return answer
end
end
如果我传入一个 null 的 case_id 或传入一个不存在的 case_id,则 'answer' 变量应该是 nil,并且数据库应该是未修改的。
我该怎么做才能拥有这种关系?
通过
在迁移中添加外键约束add_foreign_key :table, :column
有关详细信息,请参阅 ActiveRecord::Migration guide。
对于添加外键,首先添加 table 的名称(不是模型名称),这是第一个参数,第二个参数是外键的名称 table(不是模型姓名)。 作为第一个参数的 table 应该有一个名称特定的列,如下所示:name_foreign_model_id。 例子: 国外模特
class CreateGrades < ActiveRecord::Migration
def change
create_table :grades do |t|
t.string :name, limit: 20
t.integer :level, limit: 2
t.string :next, limit: 20
t.timestamps null: false
end
end
end
并且,
class CreateGoals < ActiveRecord::Migration
def change
create_table :goals do |t|
t.integer :dimension, limit: 2, null: false
**t.integer :grade_id, null: false**
t.string :description, limit: 1024, null: false
t.timestamps null: false
end
**add_foreign_key :goals, :grades**
end
end
您也可以在单独的迁移中添加外键。
有了这个,不应该接受空外键。