Search code examples
ruby-on-railspgruby-on-rails-5.2

I can not create a user through the console PG::UndefinedTable: ERROR: relation "team_members" does not exist


I can not create a user, I get an error.

I'm trying to add another model, but I can not connect it to the database, I'm getting an error and can not execute Rails db: migrate

team_members.rb

class TeamMember < ApplicationRecord  
end

I'm trying to run a command in the console and create a user but I get this error. TeamMember.create(email:"a@a.a")

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "team_members" does not exist
LINE 8:                WHERE a.attrelid = '"team_members"'::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 = '"team_members"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
from /home/*/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:63:in `async_exec'

schema.rb

ActiveRecord::Schema.define(version: 2018_09_12_142120) do

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

  create_table "active_admin_comments", force: :cascade do |t|
    t.string "namespace"
    t.text "body"
    t.string "resource_type"
    t.bigint "resource_id"
    t.string "author_type"
    t.bigint "author_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
    t.index ["namespace"], name: "index_active_admin_comments_on_namespace"
    t.index ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id"
  end

  create_table "admin_users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_admin_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_admin_users_on_reset_password_token", unique: true
  end

end

Solution

  • There is no team_members table on your database so you cannot create a TeamMember.

    If you would like to create this one you might run this command (for example);

    rails g model TeamMember email:string
    

    It will generate the migration file (db/migrate/20180914074925_create_team_members.rb) and also app/models/team_member.rb

    After rails db:migrate Then you now can create it through the console