I am trying to deploy to Heroku for the first time. During database migration I get error:
PG::UndefinedTable: ERROR: table "new_shops" does not exist
: DROP TABLE "new_shops"/app/vendor/bundle/ruby/2.1.0/gems/activerecord-new4.1.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:
in `async_exec'
new_shops is a table I thought I deleted with everything related to it (I followed instructions I've found here, although don't remember exactly what I did), but obviously I didn't.
Table itself is dropped, but something else is there. I am a newbie, so I have no idea what to do now or where to look.
What is the best way to destroy everything related to this table?
The table probably doesn't exist. You can check by running:
heroku pg:psql
\d new_shops
If it doesn't exist you should comment or remove the migration that is trying to drop the table.
Related
One of my migration files is referencing another table/model that will will be created further down the migration sequence.
Postgres doesn't like that:
PG::UndefinedTable: ERROR: relation "users" does not exist
So I wonder if there are any potential problems to manually reorder the migration files (by inventing new timestamps/prefixes)?
The affected tables are already down migrated.
When you run rake db:migrate command it compares schema_migrations table and migration files located in db/migrate folder. All the migrations which were not executed receive MigrationClass#up call then.
So starting from the point when your code is already published and/or migrations are run by other users, changing your migrations timestamps/names may lead to unprocessable migration procedure (as schema_migrations will treat a migration with changed timestamp as new, unprocessed one, and try to process it "again"). Possible workaround for this would be to comment the contents of up method for a while and uncomment it back after migrations are done. For fun you can also manipulate schema_migrations table directly from your db console (adding or removing necessary records). Both of these ways smells like a hack though.
Until then... Everything should work flawlessly.
This is what worked for me for this same situation, even though it's a hack.
Rails runs migrations in order of the timestamp, and Rails tracks which migrations have been run by the timestamp part of the migration file name, but not by the rest of the file name. So if you need to change the order of two migrations because the earlier one references the later one you can simply switch the 14 digit timestamp portion of the filenames by renaming both migration files. If the timestamp is off by even one digit Rails will think it's a new migration so write them down before changing them.
I created a new branch called user-microposts. I proceeded to mess something up on creating a model. I tried going back to the last commit using:
git reset --hard cb166caa1c70004f77eed6229168b06ad249a4ba
which gave back:
HEAD is now at cb166ca Eliminate an unnecessary assignment
So I tried redoing the Migrate, which gave me this error:
SQLite3::SQLException: table "microposts" already exists: CREATE TABLE "microposts"
....
I was surprised because I thought if I went back to the previous commit, it would be like nothing happened.
So I reverted back to master branch and deleted the branch in which I made any changes.
I then created a new branch, and tried to create another model and run rake db:migrate. Again I got the same error
SQLite3::SQLException: table "microposts" already exists: CREATE TABLE "microposts"
....
I recently tried to manually delete any file with microposts in it. Again with no such luck.
I keep getting the same error. I thought the purpose of both the git branch and/or git reset would prevent this scenario, so I am very confused. Any help would be appreciated
It would be unusual for your database to be under source control. Thus, if you revert your source you will likely have inconsistencies between the database and the source. The solution has a few options:
delete the database and recreate it
rollback the database to match the source
For both of the above you'd use the typical 'rake db' type commands.
I am getting an error when I try to migrate my db. I don't entirely remember how I got here, but I believe I:
created new branch, scaffolded 'Requests', db:migrated, switched back to master, and merged branch
created another branch, did some stuff, db:migrated, and everything was working fine.
pulled from heroku postgres database so i could test out if things worked with actual data. then tried db migrating, but gave me this error:
rake db:migrate
== CreateRequests: migrating =================================================
-- create_table(:requests)
NOTICE: CREATE TABLE will create implicit sequence "requests_id_seq1" for serial column "requests.id"
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "requests" already exists
: CREATE TABLE "requests" ("id" serial primary key, "title" character varying(255), "content" text, "category" character varying(255), "status" character varying(255), "requested_track_id" integer, "created_at" timestamp, "updated_at" timestamp)
Any ideas?
I'm not sure exactly what pull strategy you used, but if we make two reasonable assumptions about your pull strategy:
it doesn't drop the database but just overwrites tables, since this requires less permissions.
it is operating in a sort of 'archive mode', meaning it doesn't drop tables on the destination just because they don't exist on the source. Think rsync; you have to specify --delete to get what might be your expected behavior with that utility.
If your steps are correct, then what happened is you overwrote the schema_migrations table, so Rails thinks you haven't added the table yet, but neither did your heroku pull drop the table because of #2 above.
Don't create another migration!!! This will fail on everyone elses' computer except yours, but will only run on yours once.
Instead, run rails dbconsole and execute something like DROP TABLE 'requests' (I forget the postgres syntax, might not be exactly that). Then you can run your migrations.
There is another way to avoid dropping a table with data in it.
What I do in those cases is to check which migration is failing.
Suppose you have a file db/migrate/20130908214222_create_requests.rb, and for some reason, ActiveRecord failed in the past when stored this migration in its "tracking system".
To be sure that this is the case, just find, in the schema_migrations table, a row containing a number like this example: 20130908214222
If that row does not exist, you just have to insert a new one:
INSERT INTO schema_migrations(
version
) VALUES (
20130908214222
);
Next time you run rake db:migrate, ActiveRecord will omit this step, and will continue migrating to end without complications.
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
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.