I'm learning ruby on rails by following Patrick Lenz's book from Sitepoint. I know it's a bit outdated, so I'm adapting as I go along.
I've gotten to a place where I'm in my rails console, ready to start creating/saving to my database, and when I create a class, then type story.save, I get this error:
1.9.3p194 :007 > story.save
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "stories" ("link", "name") VALUES (?, ?) [["link", nil], ["name", nil]]
SQLite3::ConstraintException: constraint failed: INSERT INTO "stories" ("link", "name") VALUES (?, ?)
(0.1ms) rollback transaction
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed: INSERT INTO "stories" ("link", "name") VALUES (?, ?)
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:108:in `step'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:108:in `block in each'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:107:in `loop'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.6/lib/sqlite3/statement.rb:107:in `each'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `to_a'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `block in exec_query'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/relation.rb:66:in `insert'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/persistence.rb:363:in `create'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/timestamp.rb:57:in `create'
... 14 levels...
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/validations.rb:50:in `save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/attribute_methods/dirty.rb:22:in `save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:241:in `block in save'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/transactions.rb:240:in `save'
from (irb):7
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/ideogram/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :008 >
I have no idea what this means. Any ideas? Help is much appreciated. :)
I'm running on a Macbook Pro OSX Lion 10.7.3, Ruby 1.9.3-194, Rails 3.2.3. Have installed homebrew, rvm, and then several packages including readlines, sqlite3, and git.
THANK YOU!
Addendum: here is my schema:
ActiveRecord::Schema.define(:version => 0) do
create_table "stories", :force => true do |t|
t.string "name"
t.string "link"
end
add_index "stories", ["id"], :name => "sqlite_autoindex_stories_1", :unique => true
end
This is the sqlite command I used to create my table:
CREATE TABLE `stories` (
`id` int(11) NOT NULL,
`name` varchar(255) default NULL,
`link` varchar(255) default NULL,
PRIMARY KEY (`id`)
);
Thank you all very much for helping me out.
Between your comments, I figured out the issue. The issue was in my database -- it wasn't created correctly so once I had deleted the original file and then recompiled the file again (making sure all of my commas were in the correct place) then it finally worked!
I appreciate all your help, so thank you again. I learned quite a bit from your answers. :)
Nil values are the problem -- you can change that in the migration if you need to allow nil.
Lots of ways to verify the details from console
> story.valid?
> story.errors (after a save)
etc etc
The migration (or your db/schema.rb) would be helpful as well
stories table are not in your migration. Maybe you created it by hand (sql) with not null contraints. Since Rails have no clue about those constrains, it trows an error from your DB
From the looks of your schema, you don't have a stories table. The Patrick Lenz tutorial uses MySQL as a database but you are using SQLite3. (I was looking at an old article of his. It appears he updated the article in 2011) Since you have a schema file, I'm guessing that you are using migration files to generate your database tables. If so, your link and name fields would default to :null => true and hence would have no qualms about holding a nil value.
Is it possible that you forgot to rake db:migrate after creating the migration file for your stories table?
Related
I've been getting this persistent error every time I try to update my table in rails.
For example if my code in the schema.rb file is:
create_table "books", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
If I go into the rails console and write the command:
book = Book.new(title: "a book", description: "a book description")
it gives me the following error:
Errno::EBADF (Bad file descriptor)
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/input-method.rb:42:in `winsize'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:753:in `output_value'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:545:in `block (2 levels) in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:704:in `signal_status'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:538:in `block in eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:166:in `block (2 levels) in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `loop'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:151:in `block in each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb/ruby-lex.rb:150:in `each_top_level_statement'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:537:in `eval_input'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:472:in `block in run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `catch'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:471:in `run'
from C:/Ruby27-x64/lib/ruby/2.7.0/irb.rb:400:in `start'
Maybe IRB bug!
I can continue and write book.save which will update the new book into the database however I not sure if this error message is a concern. It wasn't there previously when running this command and is now there on all projects when trying to update any tables.
I've completely reinstalled Ruby 2.7 and RubyMine and this error still returns. I'm not sure what this problem is caused by.
Thank you for any advice.
(Info: working on Windows machine on RubyMine. Using rails 5.2.4 and Ruby 2.7.1)
That appears to be a known problem with IRB and Ruby 2.7 so could you please add your vote there:
https://youtrack.jetbrains.com/issue/RUBY-26249
I have a Rails 4.2 app on Digital Ocean. After deploying a comments model (with a migrate file), it works fine in development, but not in production.
I have been using Capistrano (per this tutorial - https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma) Deploys have been going fine until now.
This was my migrate file (missing from my deploy).
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.references :user, index: true, foreign_key: true
t.text :body
t.references :path, index: true, foreign_key: true
t.timestamps null: false
end
end
end
I checked and can see my schema in both production and dev match and refer to the last reference in the migration files.
But when
When I use rails console production, I can see that the comments table is not there. Here is the message in my console.
Loading production environment (Rails 4.2.5)
2.2.1 :001 > Comment.all
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "comments" does not exist
LINE 1: SELECT "comments".* FROM "comments"
^
: SELECT "comments".* FROM "comments"
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `async_exec'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:355:in `select'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/querying.rb:39:in `find_by_sql'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:639:in `exec_queries'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:515:in `load'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:243:in `to_a'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:630:in `inspect'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `require'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:45:in `block in exec_app_rails'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `loop'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/app_rails_loader.rb:34:in `exec_app_rails'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/cli.rb:5:in `<top (required)>'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `require'
from /home/laurie/apps/friendlyroad-rails/shared/bundle/ruby/2.2.0/gems/railties-4.2.5/bin/rails:9:in `<top (required)>'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `load'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/rails:23:in `<main>'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /home/laurie/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'2.2.1 :002 >
I have tried sudo-apt-get clean as well, I have reset my databases, added the action-pack caching gems and restarted my server several times.
My production log lists the comments view files which aren't rendering (because there is no comments table).
Running rake db:migrate:status shows "no file"
up 20151224215110 Devise create users
up 20151231173736 Add first name last name to users
up 20160112022120 Add role to users
up 20160122204744 Create paths
up 20160123004558 Add attachment image to paths
up 20160124191428 Remove user id from paths
up 20160124191601 Add user id to paths
up 20160124212622 Remove bio from users
up 20160124213023 Add bio to users
up 20160124213311 Remove title from paths
up 20160124213624 Add title content to paths
up 20160204222816 ********** NO FILE **********
checking the directory (production server) I see the file is not there.
Also, deleted old migrations, checked my server for memory, etc. Coming up blank?
I know that his is a silly question, but are you using the console with the production environment?
RAILS_ENV=production rails c
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)
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!
Can't seem to find a solution to this one. I have a relational table, BlabsUser, from which I'm trying to find and destroy a record. I'm doing this:
BlabsUser.find_by_user_id_and_blab_id(1,29).destroy
And getting this lovely error:
BlabsUser Load (0.2ms) SELECT "blabs_users".* FROM "blabs_users" WHERE "blabs_users"."user_id" = 1 AND "blabs_users"."blab_id" = 29 LIMIT 1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:234:in `block in exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:223:in `exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:249:in `exec_delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/relation.rb:351:in `delete_all'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/persistence.rb:94:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/locking/optimistic.rb:119:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `destroy'
from (irb):1
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Why is it looking for a column called blabs_users? Why isn't it just looking for a record in my blabs_users table? Also, more importantly, how do I fix this?
Edit:
And I've also tried this with the same problem:
BlabsUser.destroy_all(:blab_id => 29, :user_id => 1)
Edit 2, as per request:
My model is just
class BlabsUser < ActiveRecord::Base
end
Migration is just
class BlabsUsers < ActiveRecord::Migration
def self.up
create_table :blabs_users, :id => false, :force => true do |t|
t.integer :user_id
t.integer :blab_id
t.timestamps
end
end
def self.down
drop_table :blabs_users
end
end
ActiveRecord depends pretty heavily on having a unique key to work with (usually the primary key of the table) Since you explicitly don't have one defined (:id => false), I strongly suspect that your problem is related to that.
destroy() is also documented as Destroy an object (or multiple objects) that has the given id which strongly suggests it needs that unique id to work with.