Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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 Rails rake db:schema:加载新数据库失败_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails Rails rake db:schema:加载新数据库失败

Ruby on rails Rails rake db:schema:加载新数据库失败,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我运行了rakedb:drop(成功)和rakedb:create(成功),但是rakedb:schema:load抛出了一个我无法理解的奇怪错误 ** Invoke db:schema:load (first_time) ** Invoke environment (first_time) ** Execute environment rake aborted! PG::UndefinedTable: ERROR: relation "admins" does not exist LINE

我运行了rakedb:drop(成功)和rakedb:create(成功),但是rakedb:schema:load抛出了一个我无法理解的奇怪错误

** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted! PG::UndefinedTable: ERROR:  relation "admins" does not exist
LINE 5: WHERE a.attrelid = '"admins"'::regclass
                                      ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                    pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
               FROM pg_attribute a LEFT JOIN pg_attrdef d
                 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
           WHERE a.attrelid = '"admins"'::regclass
                AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

有什么建议吗?

我经常面临同样的问题。您以前是否曾经手动管理过数据库?我的意思是,假设您已经完成了创建迁移等工作,然后手动删除表。请检查您的迁移文件,它彼此重叠

尝试定位构成此堆栈的迁移文件。并尝试编辑它,如果需要,甚至删除它


如果我错了,请纠正我。

我不完全理解为什么,但我的一个FactoryGirl文件出现问题。我临时删除了文件,rake db:schema:load运行良好


该问题是由错误定义的FactoryGirl关联引起的。仍然不确定为什么factory girl在rake db:schema:load期间初始化。

我在继承的项目中遇到了这个问题。这与从数据库加载模型的路由约束有关,例如

constraints(foobar: /#{FooBar.pluck(:name).join('|')}|other/) do

为了快速开始,我只对块进行了注释,运行了
rake
任务,然后取消了块的注释,一切都很顺利。显然不是原始问题的问题,而是由于加载问题导致问题略有不同的同一错误的修复。

如果您使用的是FactoryBot(FactoryGirl),并且您的工厂包含任何模型类型引用,请确保它们用大括号括起来。这可以防止
db:schema:load
爆炸,并且您不必注释掉您的工厂

例如,假设您有一个带轮子和发动机的汽车模型,并且您的种子数据中有两种类型的发动机,并且使用特征指定发动机类型

此代码将爆炸
db:schema:load

FactoryBot.define do
  factory :car do
    wheels

    trait(:v8) {
      engine Engine.find_by_type('V8') 
    }

    trait(:i4) {
      engine Engine.find_by_type('I4') 
    }
  end
end
FactoryBot.define do
  factory :hot_rod, class: Car do
这将防止
db:schema:load
爆炸:

FactoryBot.define do
  factory :car do
    wheels

    trait(:v8) {
      engine { Engine.find_by_type('V8') }
    }

    trait(:i4) {
      engine { Engine.find_by_type('I4') }
    }
  end
end
以下是关于它的讨论:

更新:在为工厂指定类时也会发生这种情况:

此代码将爆炸
db:schema:load

FactoryBot.define do
  factory :car do
    wheels

    trait(:v8) {
      engine Engine.find_by_type('V8') 
    }

    trait(:i4) {
      engine Engine.find_by_type('I4') 
    }
  end
end
FactoryBot.define do
  factory :hot_rod, class: Car do
这将防止
db:schema:load
爆炸(使用符号而不是模型类型):


您是否运行了rake db:migrate?