Error migrating app to heroku - ruby-on-rails

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)

Related

Getting a PG::DuplicateTable: ERROR

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

Cannot heroku run rake db:migrate

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.

uninitialized constant SessionsHelper (NameError)

I'm doing the Hartl tutorial and my rails app works fine in development, but crashes in heroku with this error code:
application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
This happened after I added a remember_digest to the schema. Not sure if it's migrations or an issue between SessionsHelper and ApplicationController
ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
sessions_helper.rb located in app/helpers
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
#current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
#current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
#current_user = nil
end
end
I tried deleting my old heroku app and starting a new one to reset the migrations (worked to "troubleshoot" in the past) but it didnt work this time. All of the migrations are showing up when I ran heroku rake db:migrate except the remember digest one. I ran db:migrate again but can't get it to migrate. Here are migrations:
Migrate.db
20150204074511_create_users.rb 20150204093042_add_phone_number_to_users.rb
20150204081616_add_index_to_users_email.rb 20150204094519_add_index_to_users_phone_number.rb
20150204081750_add_password_digest_to_users.rb 20150207093225_add_remember_digest_to_users.rb
Migrate logs
ajhausdorf#rails-tutorial:~/workspace/AccessOBD (master) $ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.4474
(18.0ms) CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
(8.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
ActiveRecord::SchemaMigration Load (1.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateUsers (20150204074511)
(0.9ms) BEGIN
== 20150204074511 CreateUsers: migrating ======================================
-- create_table(:users)
(15.1ms) CREATE TABLE "users" ("id" serial primary key, "name" character varying, "email" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0162s
== 20150204074511 CreateUsers: migrated (0.0164s) =============================
SQL (1.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204074511"]]
(4.0ms) COMMIT
Migrating to AddIndexToUsersEmail (20150204081616)
(0.8ms) BEGIN
== 20150204081616 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
(4.6ms) CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")
-> 0.0081s
== 20150204081616 AddIndexToUsersEmail: migrated (0.0082s) ====================
SQL (0.9ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204081616"]]
(5.5ms) COMMIT
Migrating to AddPasswordDigestToUsers (20150204081750)
(0.7ms) BEGIN
== 20150204081750 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string)
(1.3ms) ALTER TABLE "users" ADD "password_digest" character varying
-> 0.0022s
== 20150204081750 AddPasswordDigestToUsers: migrated (0.0023s) ================
SQL (0.8ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204081750"]]
(1.7ms) COMMIT
Migrating to AddPhoneNumberToUsers (20150204093042)
(0.7ms) BEGIN
== 20150204093042 AddPhoneNumberToUsers: migrating ============================
-- add_column(:users, :phone, :string)
(1.4ms) ALTER TABLE "users" ADD "phone" character varying
-> 0.0023s
== 20150204093042 AddPhoneNumberToUsers: migrated (0.0024s) ===================
SQL (0.9ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204093042"]]
(2.8ms) COMMIT
Migrating to AddIndexToUsersPhoneNumber (20150204094519)
(4.4ms) BEGIN
== 20150204094519 AddIndexToUsersPhoneNumber: migrating =======================
-- add_index(:users, :phone, {:unique=>true})
(7.5ms) CREATE UNIQUE INDEX "index_users_on_phone" ON "users" ("phone")
-> 0.0110s
== 20150204094519 AddIndexToUsersPhoneNumber: migrated (0.0111s) ==============
SQL (0.8ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204094519"]]
(4.7ms) COMMIT
Is there a reason that this migration may not be going through? Running db:migrate again doesn't give any results. Any information on where you think the problem may be located even would be helpful, I cannot find any info on what's causing this.
My problem was that git wasn't tracking any of the sessions files I had added, so they were on my local computer but not on github->heroku. I found this out by making a change only on sessions_helper, then committing to git only to get a message that there were no changes to any files but several (all of the sessions files) were untracked.
Happened because I used git -am "commit message" rather than git add -A first bc I thought the -a flag added everything. Should've checked git to make sure sessions_helper.rb was there, all the answers told me to check this but I was only checking on my local machine.

Heroku App Crash err: uninitialized constant ApplicationController::SessionsHelper (NameError)

I'm doing the Hartl tutorial and my rails app works fine in development, but crashes in heroku with this error code:
application_controller.rb:5:in `<class:ApplicationController>': uninitialized constant ApplicationController::SessionsHelper (NameError)
This happened after I added a remember_digest to the schema. Not sure if it's migrations or an issue between SessionHelper and ApplicationController
ApplicationController
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
Sessions_Helper.rb
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
#current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
#current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
#current_user = nil
end
end
I tried deleting my old heroku app and starting a new one to reset the migrations (worked to "troubleshoot" in the past) but it didnt work this time. All of the migrations are showing up when I ran heroku rake db:migrate except the remember digest one. I ran db:migrate again but can't get it to migrate. Here are migrations:
Migrate.db
20150204074511_create_users.rb 20150204093042_add_phone_number_to_users.rb
20150204081616_add_index_to_users_email.rb 20150204094519_add_index_to_users_phone_number.rb
20150204081750_add_password_digest_to_users.rb 20150207093225_add_remember_digest_to_users.rb
Migrate logs
ajhausdorf#rails-tutorial:~/workspace/AccessOBD (master) $ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.4474
(18.0ms) CREATE TABLE "schema_migrations" ("version" character varying NOT NULL)
(8.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
ActiveRecord::SchemaMigration Load (1.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateUsers (20150204074511)
(0.9ms) BEGIN
== 20150204074511 CreateUsers: migrating ======================================
-- create_table(:users)
(15.1ms) CREATE TABLE "users" ("id" serial primary key, "name" character varying, "email" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
-> 0.0162s
== 20150204074511 CreateUsers: migrated (0.0164s) =============================
SQL (1.2ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204074511"]]
(4.0ms) COMMIT
Migrating to AddIndexToUsersEmail (20150204081616)
(0.8ms) BEGIN
== 20150204081616 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
(4.6ms) CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")
-> 0.0081s
== 20150204081616 AddIndexToUsersEmail: migrated (0.0082s) ====================
SQL (0.9ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204081616"]]
(5.5ms) COMMIT
Migrating to AddPasswordDigestToUsers (20150204081750)
(0.7ms) BEGIN
== 20150204081750 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string)
(1.3ms) ALTER TABLE "users" ADD "password_digest" character varying
-> 0.0022s
== 20150204081750 AddPasswordDigestToUsers: migrated (0.0023s) ================
SQL (0.8ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204081750"]]
(1.7ms) COMMIT
Migrating to AddPhoneNumberToUsers (20150204093042)
(0.7ms) BEGIN
== 20150204093042 AddPhoneNumberToUsers: migrating ============================
-- add_column(:users, :phone, :string)
(1.4ms) ALTER TABLE "users" ADD "phone" character varying
-> 0.0023s
== 20150204093042 AddPhoneNumberToUsers: migrated (0.0024s) ===================
SQL (0.9ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204093042"]]
(2.8ms) COMMIT
Migrating to AddIndexToUsersPhoneNumber (20150204094519)
(4.4ms) BEGIN
== 20150204094519 AddIndexToUsersPhoneNumber: migrating =======================
-- add_index(:users, :phone, {:unique=>true})
(7.5ms) CREATE UNIQUE INDEX "index_users_on_phone" ON "users" ("phone")
-> 0.0110s
== 20150204094519 AddIndexToUsersPhoneNumber: migrated (0.0111s) ==============
SQL (0.8ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) [["version", "20150204094519"]]
(4.7ms) COMMIT
How can I get this last migration to work?
My problem was that git wasn't tracking any of the sessions files I had added, so they were on my local computer but not on github->heroku. I found this out by making a change only on sessions_helper, then committing to git only to get a message that there were no changes to any files but several (all of the sessions files) were untracked.
Happened because I used git -am "commit message" rather than git add -A first bc I thought the -a flag added everything. Should've checked git to make sure sessions_helper.rb was there, all the answers told me to check this but I was only checking on my local machine.

ActionView::Template::Error (PG::Error: ERROR: column users.remember_token does not exist

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.

Resources