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.
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?
i'm new to Ruby. I'm at RoR Getting Started Section 5.5 and after running db:migrate, got the below error. Any advise on why? I can't find any answers or solution or problem. Pls help.
$ bin/rake db:migrate
== 20150207172154 CreateArticles: migrating ===================================
-- create_table(:articles) -> 0.0017s
== 20150207172154 CreateArticles: migrated (0.0019s) ==========================
rake aborted! StandardError: An error has occurred, this and all later
migrations canceled:
wrong number of arguments (1 for 0)-e:1:in <main>' ArgumentError:
wrong number of arguments (1 for 0)
-e:1:in' Tasks: TOP => db:migrate (See full trace by running task with --trace)
Below is my migration file.
class CreateArticles < ActiveRecord::Migration def change
create_table :articles do |t|
t.string :title
t.text :text
t.timestamps null: false
end
end
end
This is an error that I ran into once, and it's because of Arel gem, to solve it, go to Gemfile, and add this line
gem 'arel', '6.0.0.beta2'
then run bundle from the terminal. If it complains about Arel, then install it from the terminal by typing bundle update arel. Then migrate your database again.
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
I created models, views, and controllers for 'startups' each individually (without scaffolding). I have a file db>migrate>'201..._create_startups.rb' with the code below:
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps null: false
end
end
end
I ran "bundle exec rake db:migrate" and I get this response:
== 20141126011749 CreateStartups: migrating ===================================
-- create_table(:startups)
-> 0.0155s
== 20141126011749 CreateStartups: migrated (0.0159s) ==========================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 0)/Users/kevinmircovich/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
Once I run my local server and go to my browser to view my app, I have the message below:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Extracted source (around line #393):
392 def check_pending!(connection = Base.connection)
393 raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?>.>(connection)
394 end
395
396 def load_schema_if_pending!
I ran "bin/rake db:migrate RAILS_ENV=development" and had the same error as I did when I ran "bundle exec rake db:migrate":
wrong number of arguments (1 for 0)
no need to "null: false" on timestamps: it is not users' input: those are set by the active model itself, so you can remove the argument.
In Rails migration t.timestamp macro adds two columns, created_at and updated_at. These special columns are automatically managed by Active Record if they exist.
It will automatically update when new recored created & updated.
Please remove null:false argument from t.timestamp.
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps
end
end
end
I received a similar error when running rake:db migrate. To resolve my issue I ran rake:db drop to drop my database since I was in dev mode with no production database. Then I recreate the database with rake db:create after which i ran rake db:migrate successfully.
Error running rake db:migrate
ActiveRecord::PendingMigrationError
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
Resolved using:
rake db:drop - this will wipe the data out of your database
rake db:create
rake db:migrate
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.