我已经浏览了很多SO
和google
职位用于生成迁移连接表has many and belongs to many
关联并且没有任何工作。生成迁移 - 创建连接表
所有的解决方案都生成一个空的迁移文件。
我使用的是rails 3.2.13
,我有两个表格:security_users
和assignments
。这些都是一些事情我必须尝试:
rails generate migration assignments_security_users
rails generate migration create_assignments_security_users
rails generate migration create_assignments_security_users_join_table
rails g migration create_join_table :products, :categories (following the official documentation)
rails generate migration security_users_assignments security_user:belongs_to assignments:belongs_to
谁能告诉如何将两个表之间创建一个连接表迁移?
如果你这样做,确保你在字段定义中加入了null:false。例如:'''t.integer:assignment_id,null:false'''。这样可以防止最终导致无法连接的连接表的恶劣情况,数据失去完整性并且代码崩溃。 (或者最终会遇到丑陋和易出错的守卫代码)。 –
我同意@Powers并会补充说,也许这是一个想法添加索引到这些列? – BKSpurgeon