Rails migration error with id default null - ruby-on-rails

I'm trying to get a legacy Rails app up and running and when I do a
rake db:migrate
I get the following:
arx_rails [master●] % rake db:migrate
== AddSessionsTable: migrating ===============================================
-- create_table(:sessions)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `sessions` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `session_id` varchar(255) NOT NULL, `data` text, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:434:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/steve/projects/asu/arx_rails/db/migrate/20120825213542_add_sessions_table.rb:3:in `change'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `eval'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `<main>'
The migration file is:
class AddSessionsTable < ActiveRecord::Migration
def change
create_table :sessions do |t|
t.string :session_id, :null => false
t.text :data
t.timestamps
end
add_index :sessions, :session_id
add_index :sessions, :updated_at
end
end
Rails has always created the id field itself without having to have it explicitly coded in the migration file and that it makes it a primary key but without a default value. So, I'm wondering where it might be setting the id fields to a DEFAULT of NULL for newly created tables. In some configuration file somewhere I don't know about?
Has anyone seen this before?

It looks like an issue with Rails and MySQL:
https://github.com/rails/rails/pull/13247
In the notes, user "pjg" suggests the following:
With Rails 2.3.5, MySQL version 5.7.9 and mysql gem you need to have
this bit as an initializer in
config/initializers/abstract_mysql_adapter.rb:
class ActiveRecord::ConnectionAdapters::MysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
For mysql2 it should be
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
Does that resolve the issue?
(EDIT: giving credit to solution provider)

For Rails 3.0.5 works that way:
class ActiveRecord::ConnectionAdapters::ColumnDefinition
def sql_type
type.to_sym == :primary_key ? 'int(11) auto_increment PRIMARY KEY' : base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
end
end

With Rails 4.0.6 works fine:
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) NOT NULL auto_increment PRIMARY KEY"
end
$ gem list mysql
* LOCAL GEMS *
mysql2 (0.3.18)

Related

First course table to add a field open, type Boolean;rake db:migrate Operation error?

class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean
change_column_default(:courses, :open, {:from=>true, :to=>false})
end
end
!!!!!then Operation!!!!!!
rake db:migrate
== 20161126133112 AddOpenAttributeToCourses: migrating ========================
-- add_column(:courses, :open, :boolean)
-> 0.1047s
-- change_column_default(:courses, :open, {:from=>true, :to=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
: ALTER TABLE "courses" ALTER COLUMN "open" SET DEFAULT '---
:from: true
:to: false
'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:455:in `change_column_default'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:665:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:654:in `method_missing'
/home/ubuntu/workspace/db/migrate/20161126133112_add_open_attribute_to_courses.rb:4:in `change'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:608:in `exec_migration'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:591:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:590:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:768:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:959:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:823:in `up'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:801:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
You're trying to use the Rails5 arguments to change_column_default:
change_column_default(table_name, column_name, default_or_changes)
[...]
Passing a hash containing :from and :to will make this change reversible in migration:
change_column_default(:posts, :state, from: nil, to: "draft")
with Rails4:
change_column_default(table_name, column_name, default)
Sets a new default value for a column: [...]
The funny looking stuff you see in the SQL:
---
:from: true
:to: false
is the YAML version of your { :from => true, :to => false } Ruby Hash BTW.
You need to use separate up and down methods and simpler version of change_column_default with Rails4:
def up
change_column_default(:courses, :open, false)
end
def down
change_column_default(:courses, :open, true)
end
or just set the default when you create the column:
class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean, :default => false
end
end
BTW, since you didn't specify a :default when you added the column it would have a default of null, not true.
You can create the column and set a default in one line:
add_column :courses, :open, :boolean, default: true
Not sure exactly why you're getting that error. Maybe it's because you say "change the default from false to true" but the default was not false, actually it was nil.

Mysql2::Error: Table 'conversations' already exists

I'm getting this error after trying to update a gem (mailboxer) and removing it.
Mysql2::Error: Table 'conversations' already exists: CREATE TABLE conversations (id int(11) DEFAULT NULL auto_increment PRIMARY KEY, subject varchar(255) DEFAULT '', created_at datetime NOT NULL, updated_at datetime NOT NULL)
I tried:
Bundle exec rake db:migrate rollback then rake db:migrate
Bundle exec rake db:migrate v= xxxxxxxx then rake db:migrate
getting rid of the migration files (that worked but I don't like the though of just getting rid of code)
I do not want to loose my data and drop the tables.
Any thoughts?
Update:
Rake db:migrate:status
up 20140526010413 Adduserfollowme to users
up 20140703193119 Add vote weight to votes
up 20140713211140 Add partime to user
up 20140716024440 Add devise to admin users
up 20140716024510 Create active admin comments
up 20140727042518 ********** NO FILE **********
up 20140727051902 Add slug to activities
down 20140728031732 Create mailboxer.mailboxer engine
down 20140728031733 Add notified object.mailboxer engine
down 20140728031734 Add notification code.mailboxer engine
down 20140728031735 Add attachments.mailboxer engine
down 20140728031736 Rename receipts read.mailboxer engine
down 20140728031737 Add global notification support.mailboxer engine
rake db:rollback
== AddSlugToActivities: reverting ============================================
-- remove_index("activities", {:column=>:slug})
-> 0.0215s
-- remove_column("activities", :slug)
-> 0.0342s
== AddSlugToActivities: reverted (0.0559s) ===================================
Rake db:migrate
== AddSlugToActivities: migrating ============================================
-- add_column(:activities, :slug, :string)
-> 0.0266s
-- add_index(:activities, :slug)
-> 0.0104s
== AddSlugToActivities: migrated (0.0373s) ===================================
== CreateMailboxer: migrating ================================================
-- create_table(:conversations)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'conversations' already exists: CREATE TABLE `conversations` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `subject` varchar(255) DEFAULT '', `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `query'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `block in execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/foreigner-1.6.1/lib/foreigner/connection_adapters/abstract/schema_statements.rb:14:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:432:in `create_table'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:466:in `block in method_missing'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `block in say_with_time'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:438:in `say_with_time'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:458:in `method_missing'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:334:in `method_missing'
/home/bill/apps/woomatch/releases/20140728100705/db/migrate/20140728031732_create_mailboxer.mailboxer_engine.rb:6:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:370:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:410:in `block in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:389:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:528:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:777:in `call'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:777:in `ddl_transaction'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:719:in `block in migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `each'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:700:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:570:in `up'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/migration.rb:551:in `migrate'
/home/bill/apps/woomatch/shared/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
What should I do next?
There is no rake db:migrate rollback, only rake db:rollback
Try rake db:migrate:status, then use rake db:rollback to make unwanted migrations to be down.
Sometimes when 'something went wrong' and you migrate-rollback a lot, it is necessary to comment up some lines, make migrate\rollback and incomment them again.
If it doesnt work, try show code of migrations that fail

Heroku pg migration error "PG:DataCorrupted: Error"

I am getting the following error when trying to run heroku run rake db:migrate. I have tried heroku pg:reset, I have tried heroku restart. I also tried creating a new app and starting over, but the problem persists. What could be the problem here?
Running `rake db:migrate` attached to terminal... up, run.7662
Migrating to CreateChannels (20140424224543)
== CreateChannels: migrating =================================================
-- create_table(:channels)
PG::DataCorrupted: ERROR: could not read block 0 in file "base/57396/12684": read only 0 of 8192 bytes
: CREATE TABLE "channels" ("id" serial primary key, "name" character varying(255), "user_id" integer, "created_at" timestamp, "updated_at" timestamp)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DataCorrupted: ERROR: could not read block 0 in file "base/57396/12684": read only 0 of 8192 bytes
: CREATE TABLE "channels" ("id" serial primary key, "name" character varying(255), "user_id" integer, "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in `create_table'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/app/db/migrate/20140424224543_create_channels.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
[Edit] My migration file for creating the channels table looks like the following (Nothing out of the ordinary)
class CreateChannels < ActiveRecord::Migration
def change
create_table :channels do |t|
t.string :name
t.integer :user_id
t.timestamps
end
end
end
I had the same issue, I deleted the database from https://dashboard.heroku.com/apps (this was Navy)
Added a new PG Database (this was blue)
From the terminal ran:
heroku config
You should see your db url
heroku config:set DATABASE_URL=postgres://your-db-url
Ran
heroku run rake db:migrate
And everything worked.
I am still not sure why that happened!
I contacted heroku support team last night, I was having the same issue, everything is back to normal now. bellow is their response.
Unfortunately, it looks like there was an issue with your database
last night which caused an outage for a couple hours, however it's
since been recovered by one of our on-call database engineers. This
issue wasn't caused by you at all, it was a hardware failure on the
host server.
Hope this helps.
I'm now having the same issue. I didn't have any production data so will just set up a new database. Very annoying though
Edit: Has just fixed itself when I ran the migration again
Edit 2: Is still broken. When I tried to run my second migration file I get the error
PG::DataCorrupted: ERROR: could not read block 1 in file "base/117039/12684": read only 0 of 8192 bytes
Very annoying
Just tried to file a ticket on Heroku. Filled out the form with loads of information and submitted only to get a message to say that I have not created any support tickets!

Rails mountable engine: insert data rows in migration

Inside Rails mountable engine I generated a model:
rails g model Sources domain:string source_alias:string is_social:boolean is_organic:boolean organic_query_param:string
Here is migration file (20131220160128_create_sourcebuster_sources.rb)
class CreateSourcebusterSources < ActiveRecord::Migration
def change
create_table :sourcebuster_sources do |t|
t.string :domain, null: false
t.string :source_alias
t.boolean :is_social, default: false
t.boolean :is_organic, default: false
t.string :organic_query_param
t.timestamps
end
add_index :sourcebuster_sources, :domain, unique: true
add_index :sourcebuster_sources, :is_social
add_index :sourcebuster_sources, :is_organic
Sourcebuster::Source.create domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p"
end
end
Copy it to dummy app:
rake sourcebuster:install:migrations
Migration file with the different name — 20131220164445_create_sourcebuster_sources.sourcebuster.rb — but content is the same as above.
Ok, bundle exec rake db:migrate
And it gives me an error, when trying to insert data row:
== CreateSourcebusterSources: migrating ======================================
-- create_table(:sourcebuster_sources)
-> 0.0364s
-- add_index(:sourcebuster_sources, :domain, {:unique=>true})
-> 0.0066s
-- add_index(:sourcebuster_sources, :is_social)
-> 0.0069s
-- add_index(:sourcebuster_sources, :is_organic)
-> 0.0046s
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant Sourcebuster::Source/Users/Alex/Ruby/sourcebuster/test/dummy/db/migrate/20131220164445_create_sourcebuster_sources.sourcebuster.rb:17:in `change'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Why insertion fails?
try inserting this method
class CreateSourcebusterSources < ActiveRecord::Migration
def migrate(direction)
super
# Create a default Sourcebuster
Sourcebuster::Source.create! domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p" if direction == :up
end
#def change
...
end

Error to Migrate DB Rails

Im trying to use this migration but i keep getting the error, like these
This is my Migration file
class CreateEmployees < ActiveRecord::Migration
def self.up
create_table :employees do |t|
t.string :name
t.date :hiredate
t.float :salary
t.boolean :fulltime , :default => true
t.integer :vacationdays
t.text :comments
end
end
def self.down
drop_table :employees
end
end
when i try to execute rake db:migrate get this error:
== CreateEmployees: migrating ================================================
-- create_table(:employees) rake aborted! An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "employees" already exists: CREATE TABLE
"employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name"
varchar (255), "hiredate" date, "salary" float, "fulltime" boolean
DEFAULT 't', "vacationdays" integer, "comments" text)
/Library/Ruby/Gems/2.0.0/gems/sql
ite3-1.3.8/lib/sqlite3/database.rb:91:in initialize'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in
new'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in
prepare'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in
execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in block in execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:inblock in log'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in
instrument'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:inlog'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in
create_table'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:625:in
block in method_missing'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
block in say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in
method_missing'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:383:in
method_missing' /Users/YuriAguirre/Documents/Aptana Studio 3
Workspace/company/db/migrate/20130923140626_create_employees.rb:3:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in
exec_migration'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:in
block (2 levels) in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in
block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in
with_connection'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in
block in execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
block in ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
block in transactio n'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in
within_new_transact ion'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in
execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in
block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
each'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in
`block (2 levels) in ' Tasks: TOP => db:migrate (See
full trace by running task with --trace)
Your employees tables is already created in your database. You need to fix it then you can do it in two ways 1. drop the database and create one more or 2. run the down migration then up migration
rake db:drop
rake db:create
rake db:migrate
As I've written in comment, you are receiving that error because of:
SQLite3::SQLException: table "employees" already exists
Which simply means you already have that column in your database. You can resolve that in few ways:
changing migration to modify instead of create:
class CreateEmployees < ActiveRecord::Migration
def self.up
add_column :employees, :name, :string
(...)
end
end
def self.down
remove_column :employees, :name
end
end
add another migration which will drop whole table (obviously, that one should be with earlier timestamp than modifying one so table will be dropped first and then created again):
class RemoveEmployees < ActiveRecord::Migration
def change
drop_table :employees
end
end
drop table by hand from console, but that's not a very good option as you should learn to use migrations for altering your database, of course that w'd be the fastest one (if you know how to use console) and will make no harm if that's just some learning project, but still, you should learn good habits when you can.
You might also want to use newer syntax (AFAIK) def change instead of self.up and self.down, less letters to type and less duplication ;)
Your migration is not completely run.You should rollback migration and migrate again.
rails db:rollback
rails db:migrate
You ran that migration already with an error in it. So the migration ran halfway, creating the table incompletely. Rollback that migration, and then run the migration again.

Resources