Rails Migration with Change_column says successful, but silently fails - ruby-on-rails

My schema:
create_table "location_hours", force: true do |t|
t.datetime "start_at"
t.datetime "end_at"
t.integer "location_id"
t.datetime "created_at"
t.datetime "updated_at"
end
My migration:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column :location_hours, :start_at, :datetime, :null => :false
change_column :location_hours, :end_at, :datetime, :null => :false
change_column :location_hours, :location_id, :integer, :null => :false
end
end
Rake Output:
$ bundle exec rake db:migrate
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
== ChangeLocationHourNulls: migrating =======================================
-- change_column(:location_hours, :start_at, :datetime, {:null=>:false})
-> 0.0008s
-- change_column(:location_hours, :end_at, :datetime, {:null=>:false})
-> 0.0006s
-- change_column(:location_hours, :location_id, :integer, {:null=>:false})
-> 0.0032s
== ChangeLocationHourNulls: migrated (0.0067s) ==============================
-> 0.0032s
== ChangeLocationHourNulls: migrated (0.0067s) ==============================
When I check my schema file, it hasn't changed, and the database hasn't changed. Any ideas on what could cause this?

Rollback the migration ChangeLocationHourNulls.
Then change your migration as below:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column :location_hours, :start_at, :datetime, :null => false
change_column :location_hours, :end_at, :datetime, :null => false
change_column :location_hours, :location_id, :integer, :null => false
end
end
Use false and not :false.
Run rake db:migrate

I believe #Kirti advise should solve the issue but i just realized there is a better option in your case, since you just want to change Nullable option:
You could use change_column_null which works like this:
class ChangeLocationHourNulls < ActiveRecord::Migration
def change
change_column_null :location_hours, :start_at, false
change_column_null :location_hours, :end_at, false
change_column_null :location_hours, :location_id, false
end
end

Related

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

rake db:migrate issue postgresql

I'm complete noob at PostgreSQL, Ruby on Rails..
I'm trying to follow this tutorial (WITHOUT rubymine) http://www.codeproject.com/Articles/575551/User-Authentication-in-Ruby-on-Rails#InstallingRubyMine4
I have as such this to migrate (001_create_user_model.rb):
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :username, :string
t.column :email, :string
t.column :password_hash, :string
t.column :password_salt, :string
end
end
def self.down
drop_table :users
end
end
The error I'm getting goes like this:
syntax error, unexpected ':', expecting ';' or '\n'
t.column...sers do |t|
...
C:131071:in 'disable_dll_transaction'
Task:TOP => db:migrate
What about this:
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :null => false
t.string :email,:null => false
t.string :password_hash, :null => false
t.string :password_salt, :null => false
t.timestamps
end
end
def self.down
drop_table :users
end
end

add column to mailboxer migration

I wanted to add two columns to the migration that makes the notifications for the mailboxer gem. When I place both in the migration that creates the notification and then run a migration it goes through. I then input them into the form and when I submit them there are no errors and the log shows that its take them. For some reason when I try to display them they don't show up, the only thing that is showing is the original things that were part of the migration which is subject and body. My question is how can I add columns to this migration?
Here is the migration with my two columns of lat and long added to the notifications section.
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.column :lat, :text
t.column :long, :text
t.references :sender, :polymorphic => true
t.references :object, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
end
#Indexes
#Conversations
#Receipts
add_index "receipts","notification_id"
#Messages
add_index "notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
#Messages
add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
end
def self.down
#Tables
remove_foreign_key "receipts", :name => "receipts_on_notification_id"
remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :receipts
drop_table :conversations
drop_table :notifications
end
end
My show view looks like
%h1= conversation.subject
%ul
= content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt|
- message = receipt.message
%h3= message.subject
%p= message.body
%p= message.lat
%p= message.long
= render 'messages/form', conversation: conversation
This is what comes up in the console, for lat and long you see it says null
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil
I am not sure I understand exactly what you are doing, but it sounds like possibly you haven't added the two new columns to attr_accessible and they aren't being saved because of that. That's the first thing I would check (it's in the model).
Otherwise, go to the console and see if your new columns are there and see if there is data in them. That will help you find where the problem is.

rake db:migrate error wrong number of arguments (5 for 4)

This is my migration file:
class AddSeoMetaInfoToArticles < ActiveRecord::Migration
def self.up
add_column :articles, :seo_title, :string, { :default => "", :null => false }
add_column :articles, :seo_description, :string, { :default => "", :null => false }
add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }
end
def self.down
remove_column :articles, :seo_keywords
remove_column :articles, :seo_description
remove_column :articles, :seo_title
end
end
When I try to run 'rake db:migrate' I get the following error
$ rake db:migrate
AddSeoMetaInfoToArticles: migrating =======================================
-- add_column(:articles, :seo_title, :string, {:default=>"", :null=>false})
-> 0.0341s
-- add_column(:articles, :seo_description, :string, {:default=>"", :null=>false})
-> 0.0100s
-- add_column(:articles, :seo_keywords, :string, :string, {:default=>"", :null=>false})
rake aborted!
An error has occurred, this and all later migrations canceled:
wrong number of arguments (5 for 4)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I'm relatively new to rails and I'm not sure what I'm doing wrong. This is Rails 3.0.9, and a Postgres db if that makes a difference.
add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }
has :string twice, so you end up with 5 arguments being passed instead of 4.
You might also want to consider writing you migration with change - your migration is equivalent to
class AddSeoMetaInfoToArticles < ActiveRecord::Migration
def change
change_table :articles do |t|
t.string :seo_title, :default => "", :null => false
t.string :seo_description, :default => "", :null => false
t.string :seo_keywords, :default => "", :null => false
end
end
end
which I find easier on the eye. It also has the advantage that you can pass :bulk => true to change_table which will combine all 3 column additions into 1 alter table statement, which is usually much faster.
Both ways will of course work.
You give the :string argument twice in this line:
add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }

Problem with composite_primary_key Gem in Rails

I'm using the composite primary keys gem from drnic and I've got a problem with it:
If I want to create a CourseOrder (with has a cpk) with the following command in my tests: course_order = CourseOrder.new(:daily_order => daily_orders(:daily_order_one_of_today_for_hans),:course => courses(:non_veg_2), :finished => false, :ordered_at => Time.now) I'll get the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: null value in column "course_iddaily_order_id" violates not-null constraint
: INSERT INTO "course_orders" ("course_iddaily_order_id", "course_id", "ordered_at", "finished", "daily_order_id") VALUES (NULL, 489519433, '2011-03-01 10:19:27.169397', 'f', 594369222) RETURNING "course_iddaily_order_id"
I think that there is a course_iddaily_order_id is wrong, isn't it?
My Migration file looks like this:
class AddCourseOrder < ActiveRecord::Migration
def self.up
create_table :course_orders, :primary_key => [:course_id, :daily_order_id] do |table|
table.integer :course_id
table.integer :daily_order_id
table.datetime :ordered_at, :null => false
table.boolean :finished, :default => false, :null => false
end
end
def self.down
drop_table :course_orders
end
end
The resulting schema part looks like this:
create_table "course_orders", :primary_key => "course_iddaily_order_id", :force => true do |t|
t.integer "course_id", :null => false
t.integer "daily_order_id", :null => false
t.datetime "ordered_at", :null => false
t.boolean "finished", :default => false, :null => false
end
My Model looks like this:
class CourseOrder < ActiveRecord::Base
set_primary_keys :course_id, :daily_order_id
belongs_to :course
belongs_to :daily_order
end
I don't know whats wrong here, can you help?
I guess the create_table method doesn't takes an array for :primary_key option. I guess you can omit setting it.

Resources