【发布时间】:2017-05-09 07:15:01
【问题描述】:
当我启动 rails 控制台并想测试我刚刚创建的模型模板时,rails 控制台无法创建我的新模型实例。
yellow:maw thierry$ rails console
Running via Spring preloader in process 17717
Loading development environment (Rails 5.1.0)
>> Account.create(name: "John Doe", email: "john.doe@example.com", password: "1234ABCD", password_confirmation: "1234ABCD")
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "accounts" does not exist
LINE 8: WHERE a.attrelid = '"accounts"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
c.collname, col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = '"accounts"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from (irb):1
>>
但是,使用“rails db”,在同一环境(此处为开发)中执行 ActiveRecord SQL 请求会给我这个输出:
yellow:maw thierry$ rails db
psql (9.6.2)
Type "help" for help.
maw_development=> SELECT a.attname, format_type(a.atttypid, a.atttypmod),
maw_development-> pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
maw_development-> c.collname, col_description(a.attrelid, a.attnum) AS comment
maw_development-> FROM pg_attribute a
maw_development-> LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
maw_development-> LEFT JOIN pg_type t ON a.atttypid = t.oid
maw_development-> LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
maw_development-> WHERE a.attrelid = '"accounts"'::regclass
maw_development-> AND a.attnum > 0 AND NOT a.attisdropped
maw_development-> ORDER BY a.attnum;
attname | format_type | pg_get_expr | attnotnull | atttypid | atttypmod | collname | comment
-----------------+-----------------------------+--------------------------------------+------------+----------+-----------+----------+---------
id | bigint | nextval('accounts_id_seq'::regclass) | t | 20 | -1 | |
name | character varying | | f | 1043 | -1 | |
email | character varying | | f | 1043 | -1 | |
created_at | timestamp without time zone | | t | 1114 | -1 | |
updated_at | timestamp without time zone | | t | 1114 | -1 | |
password_digest | character varying | | f | 1043 | -1 | |
(6 rows)
maw_development=> SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
c.collname, col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a
LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = 'accounts'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
attname | format_type | pg_get_expr | attnotnull | atttypid | atttypmod | collname | comment
-----------------+-----------------------------+--------------------------------------+------------+----------+-----------+----------+---------
id | bigint | nextval('accounts_id_seq'::regclass) | t | 20 | -1 | |
name | character varying | | f | 1043 | -1 | |
email | character varying | | f | 1043 | -1 | |
created_at | timestamp without time zone | | t | 1114 | -1 | |
updated_at | timestamp without time zone | | t | 1114 | -1 | |
password_digest | character varying | | f | 1043 | -1 | |
(6 rows)
maw_development=>
所有迁移都已应用:
yellow:maw thierry$ rake db:migrate:status
database: maw_development
Status Migration ID Migration Name
--------------------------------------------------
up 20170505175757 Create enterprises
up 20170507120815 Create accounts
up 20170507170408 Add index to users email
up 20170507172846 Add password digest to users
yellow:maw thierry$
如您所见,我已经执行了两次请求:
- 在生成的模型名称中加入单引号 + 双引号。
- 只有单引号。
有人遇到过这个错误吗?你是怎么解决的?
提前感谢您的帮助。
上下文:
- Rails 5.1.0
- 在 macOS Sierra 上
- 使用 Rbenv。
- 使用 Ruby 2.4.1p111。
- PG 适配器:0.20.0
在模型目录中:
yellow:maw thierry$ ls -al app/models/
total 24
drwxr-xr-x 6 thierry staff 204 7 mai 14:08 .
drwxr-xr-x 10 thierry staff 340 5 mai 16:23 ..
-rw-r--r--@ 1 thierry staff 531 8 mai 12:44 account.rb
-rw-r--r-- 1 thierry staff 78 5 mai 16:23 application_record.rb
drwxr-xr-x 3 thierry staff 102 5 mai 16:23 concerns
-rw-r--r--@ 1 thierry staff 161 7 mai 18:41 enterprise.rb
yellow:maw thierry$
account.rb 的内容:
yellow:maw thierry$ cat app/models/account.rb
class Account < ApplicationRecord
before_save { email.downcase! }
validates :name, presence: true, length: { maximum: 250 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
has_secure_password
#This validation must be put after has_secure_password in order for the password field to be defined.
validates :password, presence: true, length: { minimum: 6 }
end
yellow:maw thierry$
【问题讨论】:
-
您是否重新启动了 Rails 控制台?
-
是的,很多次。
-
spring可能有问题吗? -
原来如此!谢谢@Iceman
-
春天不是第一次造成困难,因为这样的原因我很遗憾地放弃了使用它。
标签: ruby-on-rails ruby activerecord ruby-on-rails-5 rails-postgresql