foreign key

http://sevenseacat.net/2015/02/24/add_foreign_key_gotchas.html

https://robots.thoughtbot.com/referential-integrity-with-foreign-keys

保证数据库级别参照完整性

`add_foreign_key` gotchas in Rails 4.2
Feb 24, 2015 | Programming, Rails
Rails 4.2 finally added native support for database-level foreign keys, which is great. You can write the following code in a migration (assuming the presence of a users table):
def change
  create_table :posts do |t|
    t.references :user, index: true
  end
  add_foreign_key :posts, :users
end
And Rails will apply its conventions and infer what you expect:
Column   |  Type   |                     Modifiers
---------+---------+----------------------------------------------------
 id      | integer | not null default nextval('posts_id_seq'::regclass)
 user_id | integer |
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)
    "index_posts_on_user_id" btree (user_id)
Foreign-key constraints:
    "fk_rails_cbe63c1bd4" FOREIGN KEY (user_id) REFERENCES users(id)
We have a database-level foreign key pointing from the user_id column of the posts table, to the id column of the posts table. You can specify all of the expected options for the foreign key - on_delete, on_cascade, etc. This will work just fine in both PostgreSQL and MySQL - it’s a no-op in every other ActiveRecord adapter (including SQLite!)
The problem is when you want to name your associations and columns more semantically - a post doesn’t have a user, it has an author. So you generate your model nicely on the command line:
$ bin/rails g model Post author:references
Which generates a migration that includes:
def change
  create_table :posts do |t|
    t.references :author, index: true
  end
  add_foreign_key :posts, :authors
end
This will error out when you try to run the migration:
== [timestamp] CreatePosts: migrating =====================================
-- create_table(:posts)
   -> 0.0382s
-- add_foreign_key(:posts, :authors)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "authors" does not exist
: ALTER TABLE "posts" ADD CONSTRAINT "fk_rails_2cb0a9abaa"
FOREIGN KEY ("author_id")
  REFERENCES "authors" ("id")
...
Rails’ automatic inference has failed - it doesn’t know that our author association is actually to the users table.
To fix this, you can modify the migration before running it, and configure which column the foreign key should use, and which table the key should point to:
add_foreign_key :posts, :users, column: :author_id
And then the foreign key is created as you would expect:
  Column     |  Type   |                      Modifiers
-----------+---------+-----------------------------------------------------
 id        | integer | not null default nextval('posts_id_seq'::regclass)
 author_id | integer |
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)
    "index_posts_on_author_id" btree (author_id)
Foreign-key constraints:
    "fk_rails_5492f4e861" FOREIGN KEY (author_id) REFERENCES users(id)
Success! Rails can do some amazing things when it comes to following its conventions, but sometimes it requires a little help to take full advantage of the functionality it provides. I hope this helps someone!

 

转载于:https://www.cnblogs.com/znsongshu/p/6080205.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值