When I run rake db:migrate, show the following error:
-- create_table(:posts)<br>
rake aborted!
StandardError: An error has occurred, all later migrations canceled:`<br>
undefined method 'int' for #
<ActiveRecord::ConnectionAdapters::MySQL::TableDefinition:0x0055c088b9f520>
Did you mean? in?
Any ideas or suggestions?
I just change the migrate file in db/migrate from int to integer. Like this: t.integer: age
A reason this could happen is if you accidentally got a column name and a data type around the wrong way during rails generate model ....
For example, if you accidentally ran rails g model calculations references:appointment (instead of rails g model calculations appointment:references), then you'd have:
t.appointment :references
but it should be
t.references :appointment
The same mistake could happen with not just references, but any other data type too (e.g. integer, string and so on).
Related
Using Rails 4.2.6, Ruby 2.0.0, OSX 10.11
I'm trying to run the rails server. Before that, I run "rake db:migrate", then I get the following error:
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `id' for nil:NilClass
I'm quite new on Ruby. Can someone explain the error and how to solve it?
Thank you!
I just ran into this as well. The answer above wasn't very helpful in this case because so much of running the migration is meta programed in Active Record.
For me, the issue was that I had written a migration like this:
create_table :user_table do |t|
t.id :reference_table_id
end
I fixed it by changing the middle line to t.integer instead because id is not a column type.
Without seeing your migration I cannot be sure this will solve your issue, but it's what worked for me!
Look at the stack trace of the error (the stuff on the terminal after the error is displayed)
You should see a pointer to the line of code where the error is. It'll probably be something like x.id where x is the nil value
I am facing the below error when saving the record into database after I added the new column to migration that is "fathername"
NoMethodError in StudentController#admission1
undefined method `fathername=' for #<User:0x7ffcd42a3a40>
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:255:in `method_missing'
/root/ansipro342/app/models/student.rb:141:in `create_user_and_validate'
/root/ansipro342/app/controllers/student_controller.rb:62:in `admission1'
Request
Parameters:
{"student"=>{"first_name"=>"Salman",
"fatheroccupation"=>"Lecturer",
"address_line1"=>"",
"address_line2"=>"",
"country_id"=>"141",
"roll_num"=>"100",
"is_email_enabled"=>"1",
"gender"=>"m",
"fathernic"=>"",
"biometric_id"=>"",
"admission_date"=>"2015-12-11",
"date_of_birth"=>"2010-12-11",
"fathername"=>"Kamran",
"nationality_id"=>"141"}
even the fathername column already exists but its showing the error pls help, I dont know what is wrong?
I think you just have to run the migrations rake db:migrate ! This is typically happening when you try to set an attribute value and this attribute column doesn't exists in the database.
Hope it helps :)
I've defined a scaffold with some of the sqlite columns having type double. When I run rake db:migrate, it gives the following error:
undefined method `double' for #<ActiveRecord::ConnectionAdapters::TableDefinition:>
Is there another way to specify a double that Rails will understand? Should I be using a float here?
Here's the scaffold command:
rails generate scaffold shop name:string latitude:double
Try using float instead of double. Take a look here.
I'm relatively new to Rails and I'm running into an issue while trying to set up Devise. I believe the issue stems from the fact that I already generated a user scaffold before trying to install Devise, yet I don't know how to solve this problem. When I proceed through the Devise setup, I get to the step where I have to enter the following code:
rails generate devise User
That works, and I get this back from terminal:
invoke active_record
create db/migrate/20120609032448_add_devise_to_users.rb
insert app/models/user.rb
route devise_for :users
The next step is to migrate the database, which I attempt but get the following error:
== AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar(255) DEFAULT '' NOT NULL
Tasks: TOP => db:migrate
I've tried destroying the original User scaffold along with the original User migration but I keep getting stuck at this error. Help would be much appreciated!
Your new migration probably has an email column defined in it. Comment that line regarding adding an email column out and run it again. It's likely you've already got an email column in your model.
Try going to this file
db/migrate/20120609032448_add_devise_to_users.rb
and in the code where it says
change_table(:users)...
alter this to
create_table(:users)...
I'm an idiot...screwed up a migration in Rails:
thinking migrations would work like model generators (using references:modelname) I did the following:
$ rails g migration add_event_to_photos references:event
which created the migration
class AddEventToPhotos < ActiveRecord::Migration
def change
add_column :photos, :references, :event
end
end
And now my development database (SQLite3) has a references column of type event in the photos table.
And my schema.rb has a line in the middle saying:
# Could not dump table "photos" because of following StandardError
# Unknown type 'event' for column 'references'
rake db:rollback is powerless against this:
$ rake db:rollback
== AddEventToPhotos: reverting ===============================================
-- remove_column("photos", :references)
rake aborted!
An error has occurred, this and all later migrations canceled:
undefined method `to_sym' for nil:NilClass
So, how to roll back and maintain my development data in the database? I'd even be happy trashing the photos table if that's my only choice..but don't want to have to rebuild the whole thing. What to do?
btw- for anyone reading this about to make same stupid mistake...don't! Use the correct migration generator:
$ rails g migration add_event_to_photos event_id:integer
The easiest way I found to do this was to recreate the table in the schema.rb file in /db/. Afterwards I ran a rake db:reset (if it says you have pending migrations, just delete them and try again).
This took care of the problem.
Go into the database by ./script/rails dbconsole. Then type these commands:
.output dump.sql
.dump
In the file dump.sql you will have the SQL commands used to recreate and populate your database. Just edit it with your favourite editor (like vim ;-) removing or fixing the column type. You may also remove the invalid migration identifier from the schema_migrations table. Drop your database (I suggest just rename the db/development.sqlite file), create new database and read the dump file into it (using command .read dump.sql).
Now you just need to fix and run your migrations.
add an empty down method and run rake db:rollback
edit ahh that's the new migration syntax, you can replace the body with simply:
def self.down; end
which is the old syntax, or perhaps delete the body altogether (haven't tried this) and then run rake db:rollback
Just an idea, I know it's not SQLite specific you can revert to an older version schema perhaps, load it up. And try again from there? You can revert (checkout) specific files in GIT. And then do def self.down; end, as was suggested by another poster.
The problem arises because while SQLite will create the schema with whatever type you give it (in this case event it can't dump that type back to ActiveRecord.
You need to edit the sqlite_master file and change create table string (sql) to be the right thing.
You probably want to back up your table first since messing up that string will wreck your table if you do it wrong.
Here is a related rails issue