简体   繁体   中英

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

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
    end
  end
end

In my room model:

class Room < ApplicationRecord
  belongs_to :channel
  has_many :users
end

In my Channel model:

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

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 end 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 end 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 end 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" end 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" end 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" end end

These are the migration files: screenshot of migration files

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 && rails db:create && rails db:migrate .

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