简体   繁体   中英

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation “people” does not exist

I'm trying to create a new rails project. Here is my schema:

ActiveRecord::Schema.define(version: 2020_07_22_184104) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "houses", force: :cascade do |t|
    t.string "address"
  end

  create_table "persons", force: :cascade do |t|
    t.string "name"
    t.integer "house_id"
  end

end

Now, when I try to create a Person instance in rails console, I get this error:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "people" does not exist
LINE 8:                WHERE a.attrelid = '"people"'::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 = '"people"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:63:in `exec'
Caused by PG::UndefinedTable: ERROR:  relation "people" does not exist
LINE 8:                WHERE a.attrelid = '"people"'::regclass

The House model works fine. My development and test databases are also being newly created, so I don't think there's any issue with the tables. Why is this error referring to "people" relation when my table name is persons?

Plural form of word "person" is "people" that is why Rails tries to call a table with this name. Change the name of your table or set the table name in your model.

class Person < ApplicationRecord
  self.table_name = 'persons'
end

See naming conventions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM