Ruby on rails PG不可定义的错误关系用户不存在
我以前见过这个问题,但只针对rspec。我还没有创建测试,因为它对我来说太高级了,但很快我会的P 我在尝试注册/登录我的应用程序时遇到此错误。我使用Desive创建用户,还使用omniauth2登录google 这是错误Ruby on rails PG不可定义的错误关系用户不存在,ruby-on-rails,database,ruby-on-rails-3,oauth-2.0,rake-task,Ruby On Rails,Database,Ruby On Rails 3,Oauth 2.0,Rake Task,我以前见过这个问题,但只针对rspec。我还没有创建测试,因为它对我来说太高级了,但很快我会的P 我在尝试注册/登录我的应用程序时遇到此错误。我使用Desive创建用户,还使用omniauth2登录google 这是错误 ActiveRecord::StatementInvalid at /users/auth/google_oauth2/callback PG::UndefinedTable: ERROR: relation "users" does not exist LINE 5:
ActiveRecord::StatementInvalid at /users/auth/google_oauth2/callback
PG::UndefinedTable: ERROR: relation "users" does not exist
LINE 5: WHERE a.attrelid = '"users"'::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 = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
我尝试了rakedb:migrate
,但它已经被创建:在模式表中存在用户。以前有人犯过这个错误吗
数据库.yml
config=/opt/local/lib/postgresql84/bin/pg_config
development:
adapter: postgresql
encoding: unicode
database: tt_intraweb_development
pool: 5
username: my_username
password:
test:
adapter: postgresql
encoding: unicode
database: tt_intraweb_test
pool: 5
username: my_username
password:
production:
adapter: postgresql
encoding: unicode
database: tt_intraweb_production
pool: 5
username: my_username
password:
最可能的原因是您的rake使用的环境与database.yml不同于您的Web服务器。您的测试数据库尚未准备好进行rspec 为rspec准备测试数据库以修复此错误
RAILS_ENV=test rake test:prepare
它将删除、创建和添加迁移到您的测试数据库
如果rake任务被终止,则其他用户正在访问“PG::Error:Error:数据库”[your_db_test]”之类的消息将执行此任务
RAILS_ENV=test rake db:migrate
首先,您应该将所有连接从数据库中分离出来。默认情况下,您使用开发环境。然后尝试使用以下命令重置数据库:
rake db:reset
rake db:reset任务将删除数据库并重新设置它。这在功能上等同于rake db:drop db:setup
这与运行所有迁移不同。它将只使用
当前schema.rb文件的内容。如果无法回滚迁移,
rake db:重置可能对您没有帮助。要了解有关转储架构的更多信息,请参阅
模式转储和您部分
如果技巧没有帮助,请删除数据库,然后重新创建,迁移数据,如果有种子,则播种数据库:
rake db:drop db:create db:migrate db:seed
或在短时间内(从3.2开始):
因为db:migrate:reset
意味着删除、创建和迁移数据库。由于rake
的默认环境是开发,如果在规范测试中看到异常,则应按如下所示为测试环境重新创建db:
RAILS_ENV=test rake db:drop db:create db:migrate
或者只加载迁移的方案:
RAILS_ENV=test rake db:drop db:create db:schema:load
在大多数情况下,测试数据库是在测试过程中播种的,因此不需要通过db:seed
任务操作。否则,您需要准备数据库(Rails 4中不推荐使用):
然后(如果实际需要):
在较新版本的Rails上,可能会出现错误ActiveRecord::NoenEnvironmentInSchemaError,因此只需使用数据库环境集task:db:environment:set:
这通常是由ActiveAdmin中的错误引起的。以下是如何避开该漏洞: 如果您使用的是ActiveAdmin,无论PG说哪个表不存在,请注释掉ActiveAdmin rb文件的内容
例如,在这种情况下,
PGError:ERROR:relation“users”不存在
,请注释掉app/admin/users.rb的全部内容,然后在完成迁移后取消注释。我在运行rspec:
Failure/Error: it { expect(subject.priority_list).to eq [nil] * 9 }
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "priorities" does not exist
LINE 5: WHERE a.attrelid = '"priorities"'::regclass
...
我跑完后就决定了
rake db:test:prepare
rake db:test:load
在我的例子中,我不得不注释掉2个ActiveAdmin
文件。我的步骤如下:
初始错误/堆栈跟踪(注意,我们在本项目中使用了Solr
):
⇒ rkdbm
java版本“1.7.0_25”
Java(TM)SE运行时环境(build 1.7.0_25-b15)
Java HotSpot(TM)64位服务器虚拟机(构建23.25-b01,混合模式)
=>Solr已在运行
雷克流产了!
PG::未定义:错误:关系“讨论”不存在
第5行:其中a.attrelid=““讨论”::regclass
^
:选择a.attname、格式和类型(a.attTypeId、a.attTypeMod),
pg_get_expr(d.adbin,d.adrelid),a.attnotnull,a.atttyped,a.atttypmod
从pg_属性a左连接pg_属性d
在a.attrelid=d.adrelid和a.attnum=d.adnum上
其中a.attrelid=““讨论”::regclass
a.attnum>0,而不是a.attisdropped
由a.attnum订购
/Users/matthewcampbell/Sites/code/stack builders/AchieveX/app/admin/Users.rb:25:inblock in'
/Users/matthewcampbell/Sites/code/stack builders/AchieveX/app/admin/Users.rb:1:in
'
/Users/matthewcampbell/Sites/code/stack builders/AchieveX/config/routes.rb:3:inblock in'
/Users/matthewcampbell/Sites/code/stack builders/AchieveX/config/routes.rb:1:in
'
/Users/matthewcampbell/Sites/code/stack builders/AchieveX/config/environment.rb:5:in`'
任务:TOP=>db:migrate=>environment
(通过使用--trace运行任务查看完整跟踪)
我根据注释掉了app/admin/discussions.rb
文件,并再次尝试迁移我的数据库
同样的错误
我更仔细地查看了stacktrace,注意到实际上,app/admin/users.rb:25
引发了异常——而且足够肯定的是,该文件依赖于我的讨论
表(通过执行Discussion.all
)
最后,注释掉users.rb
的内容使我最终成功地迁移了数据库
仅供参考:ActiveAdmin
中讨论了gem是否应在需要时加载数据库。我在尝试使用rspec运行测试时遇到类似错误
我遵循了马萨аъСъСъъъ的步骤,但仍然没有达到目标。我需要做的最后一步是使用以下命令将模式加载到测试数据库中:
RAILS\u ENV=test rake db:schema:load
在那之后,问题消失了,我可以继续下一个bug。希望这能给你一些启示。我也有类似的错误。错误的根源是我在factories.rb文件中引用了Rails模型。因此,它导致了一个加载错误问题。修复方法是将引用包装在块或{}
中,以便延迟运行它
这是坏代码:
FactoryGirl.define do
factory :user do
guid User.new.send(:new_token)
end
end
这是错误的,因为
RAILS_ENV=test rake db:environment:set db:drop db:create db:migrate
Failure/Error: it { expect(subject.priority_list).to eq [nil] * 9 }
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "priorities" does not exist
LINE 5: WHERE a.attrelid = '"priorities"'::regclass
...
rake db:test:prepare
rake db:test:load
FactoryGirl.define do
factory :user do
guid User.new.send(:new_token)
end
end
FactoryGirl.define do
factory :user do
guid { User.new.send(:new_token) }
end
end
change_table(:users)
create_table(:users)
mount API::Base => '/'
unless ( File.basename($0) == "rake" && ARGV.include?("db:migrate") )
mount API::Base => '/'
end
rake db:migrate
heroku run rake db:migrate
FactoryGirl.define do
factory :billing_product, class: 'Billing::Product' do
name Faker::Cat.name
product_type 'fuel'
active true
payment_options [Billing::PaymentOption.new(term: 1, payment_term: 1)]
end
end
payment_options {[Billing::PaymentOption.new(term: 1, payment_term: 1)]}
ERROR["test_should_get_new", UsersControllerTest, 0.47265757399145514]
test_should_get_new#UsersControllerTest (0.47s)
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "type_zones" does not exist
LINE 1: DELETE FROM "type_zones"
^
: DELETE FROM "type_zones"
class CreateRoles < ActiveRecord # <---- Pay attention
def change
create_table :roles do |t|
t.string :name
t.integer :code, limit: 2
t.boolean :is_active, default: true
t.timestamps
end
end
end
class CreateRoles < ActiveRecord::Migration[5.0]
def change
create_table :roles do |t|
t.string :name
t.integer :code, limit: 2
t.boolean :is_active, default: true
t.timestamps
end
end
end
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist
LINE 8: WHERE a.attrelid = '"users"'::regclass
development:
adapter: postgresql
encoding: unicode
database: my_database
host: localhost
port: 5432
pool: 5
username: postgres
password: xyz
test:
adapter: postgresql
encoding: unicode
database: my_test_database
host: localhost
port: 5432
pool: 5
username: postgres
password: xyz
has_many :subcategories, through: :categories, dependent: :destroy
has_and_belongs_to_many :articles
has_many :subcategories, through: :categories, dependent: :destroy
declare association with categories here not articles (belongs_to / has_many_and_belongs_to)