简体   繁体   English

在Rails中使用add_reference时,架构图中显示的表之间没有关系

[英]No relationship between tables shown in schema diagram when using add_reference in Rails

I am working on a Rails application where I need to add some of the Foreign keys for existing columns from Rails migrations and few people working on same as well. 我正在开发一个Rails应用程序,在其中我需要为Rails迁移中的现有列添加一些外键,并且也需要很少的人来进行相同的工作。 I am adding this way: 我添加这种方式:

class AddUserRefToEvents < ActiveRecord::Migration[5.2]
  def change
    add_foreign_key :events, :users, name: :index_events_on_user_id
  end
end

and one of my mate is adding this way: 而我的一位同伴正在添加这种方式:

class AddUserToParts < ActiveRecord::Migration[5.0]
  def up
    add_reference :parts, :user
  end

  def down
    remove_reference :parts, :user
  end
end

When we use SchemaCrawler for building schema diagram, the relation is built in between user and events (with add_foreign_key ) but there is no relation between users and parts (when added add_reference ). 当我们使用SchemaCrawler构建模式图时,在用户和事件之间建立关系(使用add_foreign_key ),但是在用户和部件之间没有关系( added add_reference )。

Can you tell me why? 你能告诉我为什么吗? Can we use both (with reference to this link ) or only add_foreign_key ? 我们可以同时使用这两个(参考此链接 )还是仅使用add_foreign_key Please help. 请帮忙。

The fact is that add_reference does not add a foreign key by default. 事实是,默认情况下add_reference 不会添加外键。 The API Docs for v5.2 or v5.0 state that the options hash can include a :foreign_key key to set the appropriate foreign key constraint. v5.2v5.0的API文档指出,选项哈希可以包含:foreign_key键,以设置适当的外键约束。

So, to use add_reference just change your code to include foreign_key: true as an option. 因此,要使用add_reference只需将您的代码更改为包括foreign_key: true作为选项。

add_reference :parts, :user, foreign_key: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM