Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails PG::不可定义:错误:关系;“音乐家”;不存在_Ruby On Rails_Ruby_Postgresql_Heroku - Fatal编程技术网

Ruby on rails PG::不可定义:错误:关系;“音乐家”;不存在

Ruby on rails PG::不可定义:错误:关系;“音乐家”;不存在,ruby-on-rails,ruby,postgresql,heroku,Ruby On Rails,Ruby,Postgresql,Heroku,rakedb:migrate在sqlite3本地工作,但在heroku的postgresql中不工作 错误 PG::UndefinedTable: ERROR: relation "musicians" does not exist : ALTER TABLE "orders" ADD CONSTRAINT "fk_rails_ad134589be" FOREIGN KEY ("musician_id") REFERENCES "musicians" ("id") (0.9ms)

rakedb:migrate
sqlite3
本地工作,但在heroku的
postgresql
中不工作

错误

PG::UndefinedTable: ERROR:  relation "musicians" does not exist
: ALTER TABLE "orders" ADD CONSTRAINT "fk_rails_ad134589be"
FOREIGN KEY ("musician_id")
  REFERENCES "musicians" ("id")
   (0.9ms)  ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR:  relation "musicians" does not exist
: ALTER TABLE "orders" ADD CONSTRAINT "fk_rails_ad134589be"
FOREIGN KEY ("musician_id")
以下是指向整个日志的链接:

以下是未执行的迁移。错误显示在迁移代码下面

class CreateAlbums < ActiveRecord::Migration
  def change
    create_table :albums do |t|
      t.string :album_name
      t.references :musician, index: true, foreign_key: true
      t.timestamps null: false
    end
    add_foreign_key :albums, :users, column: :musician_id
  end
end
class CreateAlbums
我有一个用户表,其中有一个音乐家列,它是布尔值(一些用户是音乐家)

我甚至尝试过使用
添加外键
,但仍然无法找出问题所在


我尝试了
rakedb:schema:load
,结果成功了。我希望能够使rake db:migrate工作,因为我需要能够在生产环境中进行迁移。

SQLite不检查外键,它只是忽略它们。但是PostgreSQL非常严格,当外键约束无效时会引发错误

Rails不支持您希望它做的事情。当您编写
t.references:music
时,必须有一个
mummers
表。但是您希望外键指向
users

我看到两种选择:

  • 使用
    t.references:users
    并在
    albums.rb中重命名该关联,如下所示:

    belongs_to :musician, class_name: 'User', foreign_key: 'user_id'
    
  • 或者:您只需使用
    t.integer:music\u id
    而不是
    references
    ,并使用
    执行'ALTER TABLE…'手动定义外键约束。


  • @spickermann所说的是正确的。 将您的迁移更改为以下方式应该可以工作:

    class CreateAlbums < ActiveRecord::Migration
      def change
        create_table :albums do |t|
          t.string :album_name
          t.integer :musician_id
          t.timestamps null: false
        end
        add_foreign_key :albums, :users, column: :musician_id
        add_index :albums, :musician_id
      end
    end
    
    class CreateAlbums
    你是否运行过
    heroku run rake db:migrate
    ?这是不起作用的,Pavan你是否也可以在
    app/models/album.rb
    的关联部分中粘贴?`属于:音乐家,类名:User.name``有很多:歌曲``有很多:顺序项`1。我可能会选择第一种。我已经按照你建议的方式使用了协会。2.如何在heroku中手动执行
    执行'ALTER TABLE…'
    ?3.如果我删除了带有t.references的行,并在迁移中使用
    add_foreign_key
    行,它会起作用吗?将创建您提到的迁移的命令是什么?(我也计划按照Speckerman所说的做)您可以编辑迁移。现有迁移在postgresql上不起作用。您必须对现有迁移进行更改。进行此更改不会影响sqlite3所发生的事情。我请求为将来而不是现在发出命令。现在我只是接受这个建议:)