ruby 如何在Rails7中添加额外的引用到连接表迁移

v1uwarro  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(85)

下面的迁移会抛出下面的错误。我做错了什么?

class CreateJoinTableProjectsUsers < ActiveRecord::Migration[7.0]
  def change
    create_join_table :projects, :users do |t|
      # t.index [:project_id, :user_id]
      # t.index [:user_id, :project_id]

      t.references :creator, references: :users, null: false, foreign_key: true
      t.timestamps
    end
  end
end

字符串
错误:

== 20230720163543 CreateJoinTableProjectsUsers: migrating =====================
-- create_join_table(:projects, :users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "creators" does not exist
/.../db/migrate/20230720163543_create_join_table_projects_users.rb:3:in `change'

Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "creators" does not exist
/.../db/migrate/20230720163543_create_join_table_projects_users.rb:3:in `change'

Caused by:
PG::UndefinedTable: ERROR:  relation "creators" does not exist
/.../db/migrate/20230720163543_create_join_table_projects_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)


它说关系creators不存在,但我试图创建它,所以它确实不存在。

b4lqfgs4

b4lqfgs41#

我已经解决了这个问题,我传递了错误的参数。下面是实现的方法:

class CreateJoinTableProjectsUsers < ActiveRecord::Migration[7.0]
  def change
    create_join_table :projects, :users do |t|
      # t.index [:project_id, :user_id]
      # t.index [:user_id, :project_id]

      t.references :creator, null: false, foreign_key: { to_table: :users }
      t.timestamps
    end
  end
end

字符串

相关问题