添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "channels" does not exist

Ask Question

I'm trying to deploy to Heroku and I was getting the sqlite3 error so I followed the instructions on https://devcenter.heroku.com/articles/sqlite3 to switch to postgres but now I'm getting an error when I run rails db:migrate.

I've already tried dropping the database and creating it again with rails db:drop db:create, but haven't been able to get it to work.

My migration:

class CreateRooms < ActiveRecord::Migration[5.2]
  def change
    create_table :rooms do |t|
      t.string :name
      t.text :description
      t.integer :session_id
      t.references :channel, foreign_key: true
      t.timestamps

In my room model:

class Room < ApplicationRecord
  belongs_to :channel
  has_many :users

In my Channel model:

class Channel < ApplicationRecord
  has_many :rooms, dependent: :destroy
  has_many :registrations, dependent: :destroy
  has_many :users, through: :registrations

I get this error when running rails db:migrate

PG::UndefinedTable: ERROR:  relation "channels" does not exist
: CREATE TABLE "rooms" ("id" bigserial primary key, "name" character varying, "description" text, "session_id" integer, "channel_id" bigint, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_1f9c11d4ad"
FOREIGN KEY ("channel_id")
  REFERENCES "channels" ("id")
/CIS-196/final-project/db/migrate/20201128050333_create_rooms.rb:3:in `change'
bin/rails:4:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "channels" does not exist
: CREATE TABLE "rooms" ("id" bigserial primary key, "name" character varying, "description" text, "session_id" integer, "channel_id" bigint, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_1f9c11d4ad"
FOREIGN KEY ("channel_id")
  REFERENCES "channels" ("id")
/CIS-196/final-project/db/migrate/20201128050333_create_rooms.rb:3:in `change'
bin/rails:4:in `<main>'
Caused by:
PG::UndefinedTable: ERROR:  relation "channels" does not exist
/CIS-196/final-project/db/migrate/20201128050333_create_rooms.rb:3:in `change'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate

This is the schema:

ActiveRecord::Schema.define(version: 2020_12_12_224511) do
  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  create_table "channels", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  create_table "registrations", force: :cascade do |t|
    t.integer "channel_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["channel_id"], name: "index_registrations_on_channel_id"
    t.index ["user_id"], name: "index_registrations_on_user_id"
  create_table "rooms", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.integer "channel_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "session_id"
    t.index ["channel_id"], name: "index_rooms_on_channel_id"
  create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "name"
    t.string "email", default: "", null: false
    t.string "password_hash"
    t.text "bio"
    t.integer "room_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", 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.index ["room_id"], name: "index_users_on_room_id"

These are the migration files: screenshot of migration files

I guess you need to create the channels table before you create the rooms table. Can you list the file names that are in your db/migrate folder? – Fabian Winkler Dec 18, 2020 at 10:38 The files are: 20201128050215_create_users; 20201128050333_create_rooms; 20201128050352_create_channels; 20201128050445_create_registrations; 20201128062322_add_devise_to_users; 20201212090248_add_session_to_rooms; 20201212224511_create_active_storage_tables.active_storage – rgit00 Dec 18, 2020 at 22:49

Your rooms table references the channel table. Therefore you need to create the channel table before you create the rooms table. You need to rename the migration which creates the channels table so that it appears before the migration that creates the rooms table.

Thus rename 20201128050352_create_channels.rb to 20201128050332_create_channels.rb so it will run before 20201128050333_create_rooms.rb. Then run rails db:drop db:create db:migrate.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.