0

當我啓動rails控制檯並想測試剛剛創建的模型模板時,rails控制檯無法創建我的新模型實例。運行rails控制檯時發生ActiveRecord錯誤

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: "[email protected]", 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 
>> 

但是,隨着 「軌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塞拉利昂
  • 使用Rbenv。
  • 隨着Ruby 2.4.1p111。
  • PG適配器:0.20.0

在車型目錄:account.rb的

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 .. 
[email protected] 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 
[email protected] 1 thierry staff 161 7 mai 18:41 enterprise.rb 
yellow:maw thierry$ 

內容:

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+\-.][email protected][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$ 
+0

你重新啓動軌道控制檯? – Iceman

+0

是的,很多次。 – BeFree

+0

這可能是'spring'的問題嗎? – Iceman

回答

1

像@Iceman在他的評論中認爲,這是「春天相關「。所以,我已經:

  • 停止欄控制檯。
  • 用「彈簧停止」停止彈簧。
  • 確認春天真的停止了「ps aux | grep spring」,它是。
  • 並最終重新啓動「導軌控制檯」。

,其結果是:

yellow:maw thierry$ rails console 
Running via Spring preloader in process 22940 
Loading development environment (Rails 5.1.0) 
>> Account.create(name: "John Doe", email: "[email protected]", password: "1234ABCD", password_confirmation: "1234ABCD") 
    (0.1ms) BEGIN 
    Account Exists (61.2ms) SELECT 1 AS one FROM "accounts" WHERE LOWER("accounts"."email") = LOWER($1) LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]] 
    SQL (0.5ms) INSERT INTO "accounts" ("name", "email", "created_at", "updated_at", "password_digest") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "John Doe"], ["email", "[email protected]"], ["created_at", "2017-05-09 08:14:29.093633"], ["updated_at", "2017-05-09 08:14:29.093633"], ["password_digest", "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5TjUxnkPSzu"]] 
    (40.5ms) COMMIT 
=> #<Account id: 1, name: "John Doe", email: "[email protected]om", created_at: "2017-05-09 08:14:29", updated_at: "2017-05-09 08:14:29", password_digest: "$2a$10$3rCS7ynWP1r8VuPRDyQmOe0Czpm6ingOX16I6./s9a5..."> 
>>