문제

I've been tasked with creating a database diagram of a current rails web application in development. I have several relationships already created in the rails application.

  belongs_to :company

  has_many :users

I used MySQL Workbench EER Diagram tool. When I pull in the Company table and the User table, MySQL does not understand that there is a relationship between the two tables.

  1. I'm wondering if there is a performance impact with MySQL not having the relationship created.

  2. Is there a way when generating my migrations in rails, to create this relationship in MySQL?

도움이 되었습니까?

해결책

To solve your current task of creating a database diagram you can use the Rails gem suggested by rdnewman. Just follow the steps on the link below:

http://rails-erd.rubyforge.org/install.html

Answering your two other questions:

  1. There's no problem in MySQL. The thing is that MySQL Workbench needs foreign keys to create the relationships between your tables. You could do that by yourself, for instance, setting your user_id attribute from the Post model as a foreign key from the posts table. What Rails ERD does is that it loads your Active Record models and processes all their attributes and associations (has_one, has_many, belongs_to, etc.), combining it into a single diagram.

  2. In order to have this relationship shown in MySQL, you'll need to create foreign_keys on your models. However, the Rails default behaviour is that the column used to hold the foreign key on a model is the name of the association with the suffix _id added. The :foreign_key option lets you set the name of the foreign key directly.

You can find more info on this Association Basics Guide for Rails.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top