Getting a PG::DuplicateTable: ERROR and can't figure out trying what is wrong.
Running via Spring preloader in process 6376
== 20170814192757 CreateRestaurants: migrating
================================
-- create_table(:restaurants)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "restaurants" already exists
: CREATE TABLE "restaurants" ("id" serial primary key, "name" character
varying, "rating" integer, "created_at" timestamp NOT NULL, "updated_at"
timestamp NOT NULL)
Tried dropping the database and recreating it with the following.
rake db:drop db:create db:migrate
Related
I created a users table and migrated it to Heroku and it worked fine. I tried to add an admin table but something went wrong and I'm not sure what happened. Both the users and admins table are working fine in my local environment but when I push the migration to Heroku I get an error that the users table was already created:
Migrating to DeviseCreateUsers (20151119150443)
== 20151119150443 DeviseCreateUsers: migrating ================================
-- create_table(:users) PG::DuplicateTable: ERROR: relation "users" already exists : CREATE TABLE "users" ("id" serial primary key,
"email" character varying(255) DEFAULT '' NOT NULL,
"encrypted_password" character varying(255) DEFAULT '' NOT NULL,
"reset_password_token" character varying(255),
"reset_password_sent_at" timestamp, "remember_created_at" timestamp,
"sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at"
timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character
varying(255), "last_sign_in_ip" character varying(255), "created_at"
timestamp NOT NULL, "updated_at" timestamp NOT NULL) rake aborted!
StandardError: An error has occurred, this and all later migrations
canceled:
PG::DuplicateTable: ERROR: relation "users" already exists : CREATE
TABLE "users" ("id" serial primary key, "email" character varying(255)
DEFAULT '' NOT NULL, "encrypted_password" character varying(255)
DEFAULT '' NOT NULL, "reset_password_token" character varying(255),
"reset_password_sent_at" timestamp, "remember_created_at" timestamp,
"sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at"
timestamp, "last_sign_in_at" timestamp, "current_sign_in_ip" character
varying(255), "last_sign_in_ip" character varying(255), "created_at"
timestamp NOT NULL, "updated_at" timestamp NOT NULL)
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
`async_exec'
This is what is says when I checked the status of the migrations locally:
Status Migration ID Migration Name
--------------------------------------------------
up 20151103020350 Create restaurants
up 20151119150443 Devise create users
up 20160809004949 Devise create admins
Status of migrations on heroku:
Status Migration ID Migration Name
--------------------------------------------------
up 20151030033303 ********** NO FILE **********
up 20151102165952 ********** NO FILE **********
up 20151102170119 ********** NO FILE **********
up 20151103020350 Create restaurants
up 20151103160832 ********** NO FILE **********
up 20151103170143 ********** NO FILE **********
up 20151105172014 ********** NO FILE **********
up 20151114043509 ********** NO FILE **********
down 20151119150443 Devise create users
down 20160809004949 Devise create admins
When I go into the Heroku console it says I have 2 users so the users table was migrated yet the migration status is showing it as down.
I don't mind losing the data from the users table but I really don't want to lose the data from the restaurants table. Not sure how to fix this.
I think your DB already has the table, due to different migration or custom action.
Try this by command line, it worked for me!
rake db:drop
rake db:create
rake db:migrate
I am trying to migrate my app to heroku and this error came up, causing a rollback of my migration. Can anyone tell me why is there an error with date_time?
remembrance:~/rails_project/alpha-blog (master) $ heroku run rake db:migrate
Running rake db:migrate on ⬢ alpha-blog-javier... up, run.4829
ActiveRecord::SchemaMigration Load (1.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to AddDescriptionToArticles (20160816052220)
(1.7ms) BEGIN
== 20160816052220 AddDescriptionToArticles: migrating =========================
-- add_column(:articles, :description, :text)
(2.1ms) ALTER TABLE "articles" ADD "description" text
-> 0.0024s
-- add_column(:articles, :created_at, :date_time)
(4.1ms) ALTER TABLE "articles" ADD "created_at" date_time
(1.7ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "date_time" does not exist
LINE 1: ALTER TABLE "articles" ADD "created_at" date_time
It should be datetime not date_time. Read the documentation.
Change this line
add_column(:articles, :created_at, :date_time)
in your migration to
add_column(:articles, :created_at, :datetime)
I'm having issues with my app.
I am trying to run:
heroku run rake db:migrate
but I get this error:
Running rake db:migrate on pierwsza1... up, run.7908
ActiveRecord::SchemaMigration Load (22.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to AddUserIdToPins (20160515200705)
(1.9ms) BEGIN
== 20160515200705 AddUserIdToPins: migrating ==================================
-- add_column(:pins, :user_id, :integrer)
(3.6ms) ALTER TABLE "pins" ADD "user_id" integrer
(8.6ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "integrer" does not exist
LINE 1: ALTER TABLE "pins" ADD "user_id" integrer
These are the contents of the file I generated with the rails generate migration add_user_id_to_pins user_id:integer:index:
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :integer
add_index :pins, :user_id
end
end
In your migration file you have defined user_id as an integrer instead of an integer
You've just need to update your migration file with a valid type
Check this line of your logs :
PG::UndefinedObject: ERROR: type "integrer" does not exist
LINE 1: ALTER TABLE "pins" ADD "user_id" integrer
It clearly states that you have mistyped "integer" with "integrer". Please correct that in your file.
I'm on Rails 4.2 and Postgres 9.5
I was using the following migration and it is not adding the id column automatically:
class AddCharacteristics < ActiveRecord::Migration
def change
create_table :characteristics do |t|
t.string :name
t.timestamps
end
end
end
The resulting table as defined in db/schema.rb does not have an id column:
create_table "characteristics", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
The id column not being in the schema.rb is a non-issue as pointed out. However I cannot access the characteristic object from the console:
2.3.0 :003 > Characteristic.last
Characteristic Load (0.3ms) SELECT "characteristics".* FROM "characteristics" ORDER BY "characteristics"."id" DESC LIMIT 1
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "characteristics" does not exist
LINE 1: SELECT "characteristics".* FROM "characteristics" ORDER BY...
^
: SELECT "characteristics".* FROM "characteristics" ORDER BY "characteristics"."id" DESC LIMIT 1
In PSQL console this query works fine:
# SELECT "characteristics".* FROM "characteristics" ORDER BY "characteristics"."id" DESC LIMIT 1;
id | name | created_at | updated_at
----+------+----------------------------+----------------------------
1 | test | 2016-01-29 12:58:24.225279 | 2016-01-29 12:58:24.225279
Don't worry, id columns are not shown in schema.rb but they exist in the DB.
For example: https://github.com/everydayrails/rails-4-1-rspec-3-0/blob/master/db/schema.rb
The edited question about my console not working was because my console was in test, but my database query was in development
If you are using RSpec and want to keep the development and test environments in sync, you might want to add ActiveRecord::Migration.maintain_test_schema! in rails_helper.rb.
References:
https://relishapp.com/rspec/rspec-rails/docs/upgrade#pending-migration-checks
https://github.com/everydayrails/rails-4-1-rspec-3-0/blob/master/spec/rails_helper.rb#L16
I'm working through Hartl's tutorial, just finished up chapter 8 and attempted to push to Heroku. After doing so, I checked Heroku logs and found that I was getting an error:
2014-02-21T04:22:37.252893+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users" WHERE "users"."remember_toke...
2014-02-21T04:22:37.252893+00:00 app[web.1]: ^
2014-02-21T04:22:37.252893+00:00 app[web.1]: : SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' LIMIT 1
2014-02-21T04:22:37.252893+00:00 app[web.1]: LINE 1: SELECT "users".* FROM "users" WHERE "users"."remember_toke...
2014-02-21T04:22:37.252893+00:00 app[web.1]: ^
2014-02-21T04:22:37.252893+00:00 app[web.1]: PG::Error: ERROR: column users.remember_token does not exist
2014-02-21T04:22:37.252893+00:00 app[web.1]: PG::Error: ERROR: column users.remember_token does not exist
2014-02-21T04:22:37.253670+00:00 app[web.1]: Rendered layouts/_header.html.erb (3.7ms)
2014-02-21T04:22:37.253670+00:00 app[web.1]: Rendered layouts/_header.html.erb (3.7ms)
2014-02-21T04:22:37.253827+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms
2014-02-21T04:22:37.253827+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms
2014-02-21T04:22:37.255866+00:00 app[web.1]:
2014-02-21T04:22:37.255866+00:00 app[web.1]: ActionView::Template::Error (PG::Error: ERROR: column users.remember_token does not exist
2014-02-21T04:22:37.255866+00:00 app[web.1]: ^
I've provided the schema.rb to show that I have run rake db:migrate:
schema.rb:
ActiveRecord::Schema.define(version: 20140219015149) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["remember_token"], name: "index_users_on_remember_token"
end
I'm using sqlite for this particular project. I've read a few things about heroku using Postgres and case sensitivity. Unfortunately, this doesn't help my issue because I created the column label in lowercase anyways.
I'm a heavy noob and this is the first Heroku error that I have encountered. I can't even tell where the logs are tracing the error back to. Any light that you can shed is much appreciated. Please let me know if there are any other files that I need to provide.
You might have run the migration locally but have you run it on Heroku?
heroku run rake db:migrate
will do that for you
Things to check:
First, make sure the column exists in your local database (SQLite):
$ rails db
sqlite> .schema users
Next, make sure the column exists on Heroku (PostgreSQL):
$ heroku pg:psql
psql> \d+ users
Possible problem:
After you added the remember_token to your User model, you may have forgotten to commit your code to your Git repository before pushing to Heroku. Run:
$ git status
This will show you if you have any modified files that you haven't committed yet.