PG undefinedtable 错误关系用户不存在 [英] PG undefinedtable error relation users does not exist
问题描述
我之前看到过这个问题,但只针对 rspec.我还没有创建测试,因为它对我来说太高级了,但很快有一天我会!:P
当我尝试注册/登录我的应用程序时出现此错误.我使用 devise 创建用户,还使用 omniauth2 使用 google 登录.
这是错误
ActiveRecord::StatementInvalid at/users/auth/google_oauth2/callbackPG::UndefinedTable:错误:关系用户"不存在第 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.atttypmodFROM pg_attribute a LEFT JOIN pg_attrdef dON a.attrelid = d.adrelid AND a.attnum = d.adnumWHERE a.attrelid = '"users"'::regclassAND a.attnum >0 AND NOT a.attisdropped按 a.attnum 订购
我尝试了 rake db:migrate
,但它已经被创建:在模式表中存在用户.以前有人遇到过这个错误吗?
database.yml
config=/opt/local/lib/postgresql84/bin/pg_config
开发:适配器:postgresql编码:unicode数据库:tt_intraweb_development池:5用户名:my_username密码:测试:适配器:postgresql编码:unicode数据库:tt_intraweb_test池:5用户名:my_username密码:生产:适配器:postgresql编码:unicode数据库:tt_intraweb_production池:5用户名:my_username密码:
首先,您应该将所有连接从数据库中分离出来.默认情况下,您使用开发环境.然后尝试使用以下命令重置数据库:
rake db:reset
<块引用>
rake db:reset 任务将删除数据库并重新设置它.这在功能上等同于 rake db:drop db:setup.
这与运行所有迁移不同.它只会使用当前 schema.rb 文件的内容.如果无法回滚迁移,rake db:reset 可能对您没有帮助.要了解有关转储架构的更多信息,请参阅模式转储和你部分.Rails 文档
如果技巧没有帮助,删除数据库,然后重新创建它,迁移数据,如果你有种子,播种数据库:
rake db:drop db:create db:migrate db:seed
或简而言之(自 3.2 起):
rake db:migrate:reset db:seed
因为 db:migrate:reset
意味着删除、创建和迁移数据库.因为rake
的默认环境是development,如果你在spec测试中看到异常,你应该为test重新创建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 中已被弃用):
rake db:test:prepare
然后(如果确实需要):
RAILS_ENV=test rake db:seed
在较新版本的 Rails 上,错误 ActiveRecord::NoEnvironmentInSchemaError 可能会出现,因此只需在任务前添加数据库环境集任务:db:environment:set:
RAILS_ENV=test rake db:environment:set db:drop db:create db:migrate
I saw this question up before, but only for rspec. I haven't created test yet because it's too advanced for me but one day soon i will! :P
I get this error when I try to sign-up/login into my app. I used devise to create user and also omniauth2 to sign-in with google.
this is the error
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
I tried rake db:migrate
, but it already is created: in schema table users exist. Has anyone got this error before?
database.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:
At first, you shall detach all connections out of database. By default you use the development environment. Then try to reset database with the following:
rake db:reset
The rake db:reset task will drop the database and set it up again. This is functionally equivalent to rake db:drop db:setup.
This is not the same as running all the migrations. It will only use the contents of the current schema.rb file. If a migration can't be rolled back, rake db:reset may not help you. To find out more about dumping the schema see Schema Dumping and You section. Rails Docs
If the trick doesn't help, drop the database, then re-create it again, migrate data, and if you have seeds, sow the database:
rake db:drop db:create db:migrate db:seed
or in short way (since 3.2):
rake db:migrate:reset db:seed
Since db:migrate:reset
implies drop, create and migrate the db. Because the default environment for rake
is development, in case if you see the exception in spec tests, you should re-create db for the test environment as follows:
RAILS_ENV=test rake db:drop db:create db:migrate
or with just loading the migrated scheme:
RAILS_ENV=test rake db:drop db:create db:schema:load
In most cases the test database is being sowed during the test procedures, so db:seed
task action isn't required to be passed. Otherwise, you shall to prepare the database (this is deprecated in Rails 4):
rake db:test:prepare
and then (if it is actually required):
RAILS_ENV=test rake db:seed
On newer versions of Rails the error ActiveRecord::NoEnvironmentInSchemaError may be risen, so just prepend the tasks with a database environment set task: db:environment:set:
RAILS_ENV=test rake db:environment:set db:drop db:create db:migrate
这篇关于PG undefinedtable 错误关系用户不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!