Trying to get an app up and running on a new machine. May be using a slightly updated version of Ruby (2.3.4 vs 2.3.1) and the Rails version is 5.1.3.
Here's the migration:
class AddDragAndDropRules < ActiveRecord::Migration[5.0]
def change
add_column :products, :fixture_location, :string, default: "none"
add_column :products, :attaches_to, :uuid, array: true, default: []
end
end
The issue during rails db:migrate.
Error:
== 20160928162420 AddDragAndDropRules: migrating ==============================
-- add_column(:products, :fixture_location, :string, {:default=>"none"})
-> 0.0142s
-- add_column(:products, :attaches_to, :uuid, {:array=>true, :default=>[]})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
no implicit conversion of Array into String
/Users/mike/api/db/migrate/20160928162420_add_drag_and_drop_rules.rb:4:in `change'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
TypeError: no implicit conversion of Array into String
/Users/mike/api/db/migrate/20160928162420_add_drag_and_drop_rules.rb:4:in `change'
bin/rails:9:in `require'
bin/rails:9:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Anyone able to spot the error?
In case this helps anyone else - the project was upgraded to Rails 5.1.3 at some point which is causing UUID array migrations to fail due to a bug.
Issue is here (https://github.com/rails/rails/issues/30539) and workaround in 5.1.2-5.1.3 is to do this:
add_column :products, :attaches_to, :uuid, array: true, default: '{}'
Should be fixed in 5.1.4 and above.
Related
I am migrating a RoR app and issue the following command, so whats all this ActiveRecord errors:
leder#home-ryzen-desktop:~/Git/gmr_production_heroku$ ./bin/rails db:migrate
== 20170111144155 CreateArticles: migrating ===================================
-- create_table(:articles)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 3, expected 2)
/home/leder/Git/gmr_production_heroku/db/migrate/20170111144155_create_articles.rb:6:in `block in change'
/home/leder/Git/gmr_production_heroku/db/migrate/20170111144155_create_articles.rb:3:in `change'
Caused by:
ArgumentError: wrong number of arguments (given 3, expected 2)
/home/leder/Git/gmr_production_heroku/db/migrate/20170111144155_create_articles.rb:6:in `block in change'
/home/leder/Git/gmr_production_heroku/db/migrate/20170111144155_create_articles.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
This is the 20170111144155_create_articles.rb:
def change
create_table :articles do |t|
t.string :title
t.text :text
t.has_attached_file :attachment
t.timestamps
end
end
end
when I add arel gem like this accepted answer:
arel
I get the error here:
error_paste
One update: gems are installed for ruby version 2.7.1 and not my current version 3.0.1. Is this due to a previous chruby installation and not my rbenv?
In a new Rails 6 project, I have a table named object_classes with a column named ClassList_id. From schema.rb:
create_table "object_classes", force: :cascade do |t|
t.string "name"
t.integer "ClassList_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ClassList_id"], name: "index_object_classes_on_ClassList_id"
end
I've realized that the column should be named class_list_id to conform with Rails expected naming convention. Therefore, I have generated a new migration:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
rename_column :object_classes, :ClassList_id, :class_list_id
end
end
However, when I run this migration, I get the following error:
/bin/bash -c "env RBENV_VERSION=2.6.1 /home/asfarley/.rbenv/libexec/rbenv exec bundle exec ruby /home/asfarley/imgseq/bin/spring rails 'db:migrate'"
== 20200716060501 FixColumnName: migrating ====================================
-- rename_column(:object_classes, :ClassList_id, :class_list_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
What am I doing wrong here? I'm looking for an explanation that addresses specifically what is wrong here, so that I can understand how to avoid this in the future.
Hard to know whether the fault is Rails or SQLite, but the issue seems to be that I had a foreign key defined for my object_classes table pointing to a table that didn't exist. With this migration, I was able to fix the foreign key constraint and rename the column:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :object_classes, :ClassLists
rename_column :object_classes, :ClassList_id, :class_list_id
add_foreign_key :object_classes, :class_lists
end
end
your syntax is ok so the error must be a reserved name error. may try and remove the column and creating the column again, watch out for capital letters on migrations for they can unnecessarily complicate your code. try this:
rails g migration RemoveClassListIdFromObjectClasses
that will created a migration kinda like this:
def drop
remove column :object_classes, :class_lis_id
end
and then run
rails g migration AddClassListIdToObjectClasses
the migration should look something like this:
def change
add_column :object_classes, :class_list_id
end
remember to add the data type and anything else you may need.
there you can check the migration it created and you can change it to maybe add the data type or maybe change the capitalization of the column and you will be set to go.
note: if it works i would appreciate if you accepted the answer, Thanks!
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :user
t.timestamps
end
end
end
class CreateArticles < ActiveRecord::Migration[6.0]
def change
create_table :articles do |t|
t.string :content
t.timestamps
end
end
end
I created these two entities in a new project and when I run migrate I get this error:
rails db:setup
Created database 'rails_api_development'
Created database 'rails_api_test'
C:/Users/admin/Desktop/projects/rails-api/db/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter C:/Users/admin/Desktop/projects/rails-api/config/application.rb to limit the frameworks that will be loaded.
admin#Desktop MINGW32 ~/Desktop/projects/rails-api (master)
$ rails db:migrate
rails aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 1000 bytes
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
Mysql2::Error: Specified key was too long; max key length is 1000 bytes
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Also, why does it say mysql2 even though I entered mysql as the database? What the heck is wrong with rails? Is it a problem with the ORM or some other config issues?
SET GLOBAL default_storage_engine = 'InnoDB';
Running this in phpmyadmin solved it. Seems to be an issue with MySQL database.
Have a little issue going on, not too sure what I've done but I just created a rails application followed by these commands.
I ran:
rails generate scaffold Post heading body:text price:decimal neighborhood external_url timestamp
in my terminal followed by:
rake db:migrate
Next I get an error that reads:
== 20150108012341 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0021s
== 20150108012341 CreatePosts: migrated (0.0022s) =============================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 0)/Users/taimurknaziri/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.0.beta2/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
/Users/taimurknaziri/.rvm/gems/ruby-2.1.1/gems/activerecord-
...
4.2.0.beta2/lib/active_record/tasks/database_tasks.rb:135:in `migrate'
/Users/taimurknaziri/.rvm/gems/ruby-2.1.1/gems/activerecord-4.2.0.beta2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/Users/taimurknaziri/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
/Users/taimurknaziri/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Migration file:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :heading
t.text :body
t.decimal :price
t.string :neighborhood
t.string :external_url
t.string :timestamp
t.timestamps null: false
end
end
end
This is what the command should be
rails generate scaffold Post heading:string body:text price:decimal neighbourhood:string externalurl:string timestamp:string
You should be mentioning the data types for all the fields .
The problem was in my gem file, my rails version was rails 4.2 beta 2. I found the solution here:
Can't migrate database after scaffold. Section 2.2 Ruby on Rails Tutorial Michael Hartl
I had to add the gem arel and run bundle update arel followed by bundle install.
I'm adding a 'questions' table to my database...when I migrated it...everything seemed fine.
BUT when I go into rails console...it tells me 'NameError: uninitialized constant Question'
what am I missing? Here is my migration output, migration file data, and rails console error.
I'm a beginner at Rails so please forgive my noobness.
So I got this after my rake db:migrate...
Zach:FundAnEd zach$ rake db:migrate
== DropQuestionTable: migrating ==============================================
-- drop_table(:questions)
-> 0.0490s
== DropQuestionTable: migrated (0.0492s) =====================================
== CreateQuestions: migrating ================================================
-- create_table(:questions)
-> 0.0161s
-- add_index(:questions, :scholarship_id)
-> 0.0254s
== CreateQuestions: migrated (0.0417s) =======================================
and this is my migration file content...
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :question_type
t.references :scholarship
t.string :question_title
t.timestamps
end
add_index :questions, :scholarship_id
end
end
AND here is the error I'm getting....
NameError: uninitialized constant Question
from (irb):1
from /Users/zach/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /Users/zach/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/zach/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
What am I doing wrong? What am I missing?
First of all you have to make sure you have a Question model, plus if you have it then make sure you have followed proper naming conventions, models in ruby on rails are singular means the model name would be "Question" not "Questions". hope it would help