加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 导轨中的外键4

发布时间:2020-12-17 03:37:26 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 4和SQLite.我正在尝试将foreing键添加到我的指标表中.请参阅下面的代码 class Indicator ActiveRecord::Base belongs_to :objetive belongs_to :responsible,class_name: "Person"end 迁移脚本: class AddFksToIndicator ActiveRecord::Mig
我正在使用Rails 4和SQLite.我正在尝试将foreing键添加到我的指标表中.请参阅下面的代码

class Indicator < ActiveRecord::Base
  belongs_to :objetive
  belongs_to :responsible,class_name: "Person"

end

迁移脚本:

class AddFksToIndicator < ActiveRecord::Migration
  def change
      add_reference :indicators,:objective,index: true
      add_reference :indicators,:responsible,index: true
  end
end

在运行迁移时一切正常,所以我尝试在控制台中:

2.0.0p247 :002 > i = Indicator.new
 => #<Indicator id: nil,name: nil,created_at: nil,updated_at: nil,objective_id: nil,responsible_id: nil> 
2.0.0p247 :002 > i.objective_id = 0
2.0.0p247 :003 > i.save

令我惊讶的是,指标已经保存,并且没有id = 0的目标.

最后,我检查了指标表模式,得到:

CREATE TABLE "indicators" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"name" varchar(255),"created_at" datetime,"updated_at" datetime,"objective_id" integer,"responsible_id" integer);
CREATE INDEX "index_indicators_on_objective_id" ON "indicators" ("objective_id");
CREATE INDEX "index_indicators_on_responsible_id" ON "indicators" ("responsible_id");

为什么在objective_id和responsible_id上??没有外键约束?
难道我做错了什么?

解决方法

根据 Ruby on Rails 4.2 Release notes,只有mysql,mysql2和postgresql适配器支持外键.

不幸的是,sqlite3适配器没有.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读