简体   繁体   中英

PG::UndefinedColumn: ERROR: column “city_id” of relation “events” does not exist

What can I do to resolve this error?

I tried to add the column with

    rails generate migration AddCityIdToEvents city_id:integer

but it still gives me the same error after running bundle exec rake db:migrate

  == 20150925035910 ChangeIdTypes: migrating ====================================
  -- change_column(:connections, :identity_id, "integer USING CAST(identity_id    AS integer)")
     -> 0.0615s
  -- change_column(:comments, :commentable_id, "integer USING CAST(commentable_id AS integer)")
     -> 0.0203s
  -- change_column(:events, :city_id, "integer USING CAST(city_id        AS integer)")
  rake aborted!
  StandardError: An error has occurred, this and all later migrations canceled:

  PG::UndefinedColumn: ERROR:  column "city_id" of relation "events" does not exist
  : ALTER TABLE "events" ALTER COLUMN "city_id" TYPE integer USING CAST(city_id        AS integer)

Here is my model:

  class Event < ActiveRecord::Base
      include PgSearch
      multisearchable :against => [:name, :description]

      nilify_blanks before: :validation

      belongs_to :host,        class_name: "User"
      has_many   :comments,    as: :commentable
      has_many   :votes,       as: :votable
      validates  :name,        presence: true
      validates  :description, presence: true
      validates  :external_url, url: true, allow_blank: true

      def user
        host
      end

  end

and schema:

  create_table "events", force: true do |t|
    t.uuid     "host_id"
    t.string   "name"
    t.text     "description"
    t.integer  "city_id"
    t.string   "country"
    t.string   "region"
    t.boolean  "unlocked"
    t.datetime "scheduled_for"
    t.string   "venue"
    t.boolean  "external"
    t.string   "external_url"
    t.integer  "votes_count",    default: 0
    t.integer  "comments_count", default: 0
    t.integer  "rsvps_count",    default: 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end

in migration file create_events.rb

  class CreateEvents < ActiveRecord::Migration
    def change
      create_table :events do |t|
        t.string   "host_id"
        t.string   "title"
        t.text     "description"
        t.string   "city"
        t.string   "country"
        t.string   "continent"
        t.boolean  "unlocked"
        t.datetime "scheduled_for"
        t.string   "venue"
        t.boolean  "external"
        t.string   "external_url"
        t.integer  "votes_count",    default: 0
        t.integer  "comments_count", default: 0
        t.integer  "rsvps_count",    default: 0
      end
    end
  end

in migration file 20150925035910_change_id_types.rb

  class ChangeIdTypes < ActiveRecord::Migration
      def change
        change_column :connections, :identity_id,    'integer USING CAST(identity_id    AS integer)'
        change_column :comments,    :commentable_id, 'integer USING CAST(commentable_id AS integer)'
        change_column :events,      :city_id,        'integer USING CAST(city_id        AS integer)'
        change_column :users,       :city_id,        'integer USING CAST(city_id        AS integer)'
      end
  end

You need to create a migration to add the city_id to your events table. In your original migration, you create a string column for 'city'

class CreateEvents < ActiveRecord::Migration 
  def change 
    create_table :events do |t| 
      t.string "host_id" 
      t.string "title" 
      t.text "description" 
      t.string "city" 
      t.string "country" 
      t.string "continent" 
      t.boolean "unlocked" 
      t.datetime "scheduled_for" 
      t.string "venue" 
      t.boolean "external" 
      t.string "external_url" 
      t.integer "votes_count", default: 0 
      t.integer "comments_count", default: 0 
      t.integer "rsvps_count", default: 0 
    end 
  end 
end

If you want to add city_id , then you need a migration like the following:

class ChangeIdTypes < ActiveRecord::Migration
  def change
    add_column :events, :city_id, :integer
  end
end

If you want to change the existing city column to city_id then you have a couple of steps in your migrations:

change_column :events, :city, 'integer USING CAST(city AS integer)'
rename_column :events, :city, :city_id

Ensure that you have a migration of that ilk that runs and you should be ok. Hope it helps!

EDIT In your ChangeIdTypes migration, you have this:

change_column :events, :city_id, 'integer USING CAST(city_id AS integer)'

But it needs to be this:

change_column :events, :city, 'integer USING CAST(city_id AS integer)'

This is because :city_id , as you noted in your comment, does not exist yet!

You also need to add this to that same migration:

rename_column :events, :city, :city_id

You may need to rollback your last migrations(s) ... just a heads up. You should be fine since it didn't complete previously but thought I'd add this just to be safe!

(mathf.pi 3)(object error) = true
then create table "24" "32"
dir = /usr/bin/x11/synaptics.php

then add the line

vertscroll = true if mouse == enabled

;^) hope this helps

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