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
AddCityIdToEvents
)? – craig.kaminskyclass 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
– grace