this is a very silly question I think: I just removed a table named person_emails I created a minute ago in a new demo app I created half an hour ago. Then I started testing like just now, when I ran a unit test on an unrelated model called line_item, and I got "ActiveRecord::StatementInvalid: Mysql::Error: Table 'depot_test.person_emails' doesn't exist: DELETE FROM person_emails"
I did do the rake db:test:prepare and rake db:migrate RAILS_ENV=test.
Also, I had this column named "price" in the line_items table which I used a migration to remove, but the test tests for it anyway and throws and error. Is there something that I always should do for tests after reverting a migration or using a migration to remove a column?
Any ideas?
Thank You!
I'm pretty sure this is happening because you still have a person_emails fixture file! It's trying to clear the table before it loads the fixture data. Check for a file called test/fixtures/person_emails.yml and delete it, and you should be set.
Related
I created a model with a column named errors and when I ran the test I naturally got an ActiveRecord::DangerousAttributeError
so I rolled back the migration and changed the column name to error_messages and reran the migration. I don't refer to that column yet in any other part of my code.
Now I can create valid objects from this model in the rails console, but still the test is giving the same error. How do I make this error go away?
I needed to refresh the database structure in the testing environment like this:
bundle exec rake db:reset RAILS_ENV=test
I accidentally deleted table "results" from schema with this command in rails console:
ActiveRecord::Migration.drop_table(:results)
I tried but not working
rake db:migrate:up VERSION=201608021358
How can I take it back? I just want the structure not the data
Please help!
You dropped the table directly without migration so there is no neat way.
Comment everything from down method from 201608021358_xxxx.rb assuming this migration is create_results
Reason for commenting the code is "Your migration is still UP as you haven't done it through migration" and if you UP it it will not do anything while DOWN will give you error saying results table doesn't exist
rake db:migrate:down VERSION=201608021358
rake db:migrate:up VERSION=201608021358
that will solve the issue
New to the rails world. Using 4.2.4.
I'm trying to create a model for my app and write some unit tests for it, but I'm running into difficulty after running a change migration. I created a model with
rails generate model player first_name:string last_name:string dispaly_name:string
and ran rake db:migrate RAILS_ENV=test
Then I wrote a (failing) unit test to make sure the proper fields were set when calling save. At this point, I realized that I misspelled dispaly_name, so I created a change migration that fixed the column name.
Now when I try to run the unit test rake test test/models/player_test.rb, I get an error (UndefinedColumn) that says the save failed because the Player model is still trying to save with the dispaly_name instead of display_name. I ran rake db:migrate rake db:migrate RAILS_ENV=test rake db:test:prepare and ran rails c and RAILS_ENV=test rails c and checked that the column name was set properly by running Player.column_names. I also checked schema.rb. Everything seems to be in order, but I can't figure out why the Player.new in my test case is using the old column name.
Can someone explain what's happening?
I got it:
when you create model then a fixture along with test file is created.
you created your model with field name dispaly_name, and in file YourApp/test/fixtures/player_test.yml a field dispaly_name is created. right?.
but the problem is that when you change field name to display_name it changes in MODEL and TABLE too but not in fixture file. so you need to correct your field in fixture file too.
please change field name dispaly_name in file YourApp/test/fixtures/player_test.yml
I hope it will work :)
Depending upon what you are using to populate your test db, FactoryGirl, Fabrication or the default Fixtures a Rails application come up with, change the data field name there. Then the error will disappear.
Do try restarting the server just in case if the error persists. I dont think so restarting the server will have any effect but just for a try.
Try to run bundle exec rake:test:clone, this recreates test db schema from current development one.
When I ran bundle exec rake db:test:prepare I got the following:
rake aborted!
Multiple migrations have the name CreateMicroposts
To check the status of my migration files, I ran
rake db:migrate:status
And got:
Status Migration ID Migration Name
------- --------------- -----------------
up 20120616205407 Create users
up 20120622103932 Add index to users email
up 20120622114559 Add password digest to users
up 20120628095820 Add remember token to users
up 20120704123654 Add admin to users
down 20120706103254 Create microposts
up 20120707073410 Create microposts
As you can see, I have two migration files with the exact same names and the exact same code in them. It's only their statuses differ, i.e. Up and Down.
What does Up and Down signify?
And which one can I delete, if I have to?
The problem is that you have two different migration files containing the header
class CreateMicroposts< ActiveRecord::Migration
rake db:migrate:status does not check the status of your migration files. It tells you what migrations will be applied if you run rake db:migrate. The up/down labels are pretty much self-explanatory: it tells you whether the migration will be applied via the up method or the down method. The up method is ran when you migrate and the down when you rollback a migration. You can make some further reading about Rails migrations here.
up is the method called when "evolving" (ie migrating to a new schema), while down is the method called when "regressing" (ie migrating to an older schema version, because one of your changes doesn't suit you). db:migrate calls up, db:rollback calls down. In recent versions of rails, there's change that handles both at the same time.
As for the deletion... I don't do activerecord much these days, but I think you're free to do whatever you want with your files. I don't think deleting a duplicate file will do any harm, and if it does.. Well, you use source control, right ? :)
If I have two migration files:
20110414132423_insert_bulk_data.rb #1st
20111122105951_add_some_columns.rb #2nd
and I run rake db:migrate, is the 1st one run firstly since it has older timestamp??
Since I am in the middle of someone else's code, he made the 20110414132423_insert_bulk_data migration which insert data to table, but this migration file complains an unknown column in the table, then I found the missing column is defined in the 2nd 20111122105951_add_some_columns.rb migration file which has newer timestamp...
How can I get rid of this?
Shortly, yes. The timestamp is used to order migrations and to navigate between them. See more here
delete this migrations
generate two new migrations in the way you need to run