PG :: UndefinedColumn:ERROR:column" city_id"关系"事件"不存在

时间:2015-10-23 20:18:18

标签: ruby-on-rails postgresql ruby-on-rails-4

我该怎么做才能解决此错误?

我尝试用

添加列
    rails generate migration AddCityIdToEvents city_id:integer

但运行后仍然会给我同样的错误 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)

这是我的模特:

  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

和架构:

  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

在迁移文件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

在迁移文件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

2 个答案:

答案 0 :(得分:2)

您需要创建迁移以将city_id添加到您的事件表中。在原始迁移中,您将为“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

如果您想添加 city_id,则需要进行如下迁移:

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

如果您想更改现有的city列到city_id,那么您在迁移中有几个步骤:

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

确保您运行的是同类网络的迁移,您应该没问题。希望它有所帮助!

修改ChangeIdTypes迁移中,您拥有以下内容:

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

但它必须是这样的:

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

这是因为您在评论中提到的:city_id尚不存在!

您还需要将此添加到同一个迁移中:

rename_column :events, :city, :city_id

您可能需要rollback上次迁移...只是抬头。你应该没事,因为它以前没有完成,但我想加上这只是为了安全!

答案 1 :(得分:-4)

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

然后添加行

vertscroll = true if mouse == enabled

; ^)希望这有帮助