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.
Related
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!
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.
I am a total newbie to Rails, and I am following the Railsbridge Intro to Rails. I have created my project and now I am trying to create a new controller action for voting, and a new route for voting. When I go to the development page, it shows the following message:
Migrations are pending. To resolve this issue, run: bin/rake
db:migrate RAILS_ENV=development
In the command line it gives me this information:
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `migrate' for #<ActiveRecord::ConnectionAdapters::TableDefiniti
on:0x5d79e78>C:/Sites/railsbridgejan/suggestotron/db/migrate/20150129195744_create_votes.rb:6:in `block in change' C:/Sites/railsbridgejan/suggestotron/db/migrate/20150129195744_create_votes.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
So, when I look in my db files for create_votes.rb this is what it looks like:
class CreateVotes < ActiveRecord::Migration
def change
create_table :votes do |t|
t.integer :topic_id
t.string :rake
t.migrate :db
t.timestamps null: false
end
end
end
Is there something wrong with my file's code? t.migrate :db is line 6, which according to the command line, is the problem. I am using Rails 4, Ruby 2, and Sqlite. I had tried to install MySql, but I ran into some major issues, so I just continued with Sqlite. Could that be causing this problem at all? It seems like the issue is in the code listed above, but I'm not sure.
Thank you!
Inside the change method you use
t.migrate :db
The migrate data type doesn't exist. I assume you wanted to use a String.
t.string :db
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.
am currently working on a rails project. When i tried to start rails server its throwing the following error:
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters
/sqlite_adapter.rb:439:in `table_structure': Could not find table 'dbrick'
(ActiveRecord::StatementInvalid)
My table name is 'dbrick'. I also Tried to rake db:drop and rake db:mirgrate. While migrating its throwing the following error:
rake aborted!
Could not find table 'dbrick'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
This is my migrate file:
class CreateDbricks < ActiveRecord::Migration
def self.up
create_table :dbricks do |t|
t.text :description
t.string :video
t.string :video_html
t.string :image_id
t.string :option_id
t.boolean :choice
t.string :reach
t.integer :category_id
t.string :user_id
t.datetime :deleted_at
t.timestamps
end
end
def self.down
drop_table :dbricks
end
end
It will be so much help full if any one help me out of this.
Thanks in advance.
I would try :
rake db:schema:load
To load your schema ( to which I believe its finding the error against your DB ).
If that fails, I would manually find the migration that creates your dbrick, locate the name of the file and copy and paste the number in the filename to produce this :
rake db:migrate:down VERSION=123412341234 # <-- where the number is the number you pasted
Look for errors. Occasionally one thing exists already, or doesn't exist already and prevents the migration from running all the way, and consequentially that would be the source of your error. If it goes successfully then rake it back up :
rake db:migrate:up VERSION=123412341234 # <-- where the number is the number you pasted
If it doesn't go successfully, then you'll have to put on your miner's helmet, and get your hands dirty with :
rails dbconsole
Which will take you into your database and you'll have to manually delete whatever table/column is preventing the migration from occurring. Once that is fixed, exit out and rake db:migrate:up!
Have you migrated your database? rake db:migrate
If you have, drop your database (this deletes all data, so be careful - do it if you do not care about losing data in your db)
rake db:drop
This will clear out your database, and your schema. Then
rake db:migrate
This will re-migrate your schema.