Error trying to migrate PG database hosted on Heroku -Rails - ruby-on-rails

Im getting this error when running heroku run rake db:migrate -> ERROR: column "frequency" cannot be cast automatically to type integer I'm not sure what I'm supposed to do in order to fix this error. Here is my schema local that works fine when I migrated:
schema:
create_table "assignments", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "account_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "attachment"
t.integer "finished_count"
t.datetime "start_at"
t.datetime "end_at"
t.integer "frequency"
t.integer "status", default: 0
t.index ["status"], name: "index_assignments_on_status"
end
Cannot I not use integer as a type with PG? if not, what should I do instead?

Without seeing your migration, which you should post. A likely cause is that the frequency column already has data in it. Therefore PG cannot blindly convert that data into a integer if it is not something that is like a number.

Related

can not create database when using "rails db:migrate" command

I am newbie with Rails and trying to make a very simple Rails program. My problem is, when I made a Rails program, at first, I had a mistake and created a database named image by this command rails g scaffold image video title star:integer description:text then I made this command rails db:migrate, when I realized that problem, I deleted image folder and create another folder named Course by this command rails g scaffold image video title star:integer description:text. Everything seem ok.
But, my problem is, when I tried this command rails db:migrate, I saw that nothing happen.
When I accessed the website http://localhost:3000/courses/new to try to make a new thing, I saw that I can not make anything new.
So, I think that maybe my problem is, I made the image folder before the course folder.
How can I work with this problem ?
Could you please give me some ideas ? Thank you very much.
#Juanse Gimenez here is my schema.rb, sorry for my shortcomings
ActiveRecord::Schema.define(version: 2021_01_08_042733) do
create_table "courses", force: :cascade do |t|
t.string "image"
t.string "video"
t.string "title"
t.integer "star"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "images", force: :cascade do |t|
t.string "video"
t.string "title"
t.integer "star"
t.text "description"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
end
#borjagvo thank you for your comment, here is my new.html.erb <h1>New Course</h1> it is very simple, so I do not think that it is the reason ?

Why am I getting "PG::NotNullViolation null value in column "id" violates not-null constraint" whenever I login in my rails app?

After dumping, downloading and pg_restoring a DB from Heroku, any time I try to login in my rails application I get the above error.
It somehow seems that the user id is seen as nil (even though I can see an id value in the DB).
I also read that it may be due to the id being a simple int instead of a serial, however, as you can see in my schema:
create_table "users", id: :serial, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "last_name"
t.string "provider"
t.string "uid"
t.boolean "admin", default: false, null: false
t.boolean "affiliate_approved", default: false, null: false
t.integer "cart_id"
t.float "rev_share"
t.boolean "notice", default: false, null: false
t.string "iban"
t.string "piva"
t.string "invitation_token"
t.datetime "invitation_created_at"
t.datetime "invitation_sent_at"
t.datetime "invitation_accepted_at"
t.integer "invitation_limit"
t.string "invited_by_type"
t.integer "invited_by_id"
t.integer "invitations_count", default: 0
t.string "username"
t.string "slug"
t.integer "enrolls_count", default: 0
t.integer "partner_id"
t.string "country"
t.integer "referrer"
t.boolean "gdpr_marketing"
t.boolean "privacy_policy"
t.boolean "subscription", default: false
t.date "account_created_at"
t.boolean "profilazione"
t.boolean "terms_and_conditions"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
t.index ["invitations_count"], name: "index_users_on_invitations_count"
t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
And it seems that the id is used already as serial.
Do you have any idea of what may be going wrong? This application is live in production with a couple of thousands of users who aren't being impacted by this issue, which makes me think there's something wrong with my local PG setup.
Edit - full error message
PG::NotNullViolation at /auth/google_oauth2/callback
ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 9367, 127.0.0.1, 2018-09-17 09:51:59.463125, 2018-09-17 09:51:59.463125).
Edit - more findings
In user.rb (the model file) there's an after_update hook where the following is executed:
def change_log
UserChange.create(ip_request: current_sign_in_ip, user: self)
end
This is so that we can track everything a user changes along with their IP (GDPR reasons).
After commenting that out, it all works alright and my user gets logged in as planned
There must be a callback to another table which is trying to persist with a null ID. Check for callbacks and the schema for any related tables. In your case this was the UserChange table.
I just had the same error after pulling a copy of the DB from Heroku:
PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains ...etc
It was happening every time I tried to create a new record. I'm guessing that when you log in, your app is creating a new record somewhere (possibly some login tracking/logging you are doing?) that is triggering the error. If you can't access the app through a web browser, you could try opening up the Rails console and see what happens if you try to create a new record directly. If you still get the error, the issue is not actually with your login but with your DB.
The issue for me was that I was using a different version of Postgres on my dev machine to the version on Heroku that created the dump file. After switching to the matching version of Postgres an re-pulling the DB, everything is working as expected again.
If you're using Postgres on Mac, when you add a new Postgres server, you can choose which version of Postgres it should use. You want to make your development and production environments as similar as possible so you definitely want to make sure that your DB versions match.

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "categories" does not exist

I am building a small rails app. When, I run heroku run rake db:migrate, I get this error
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "categories" does not exist
: CREATE TABLE "habits" ("id" serial primary key, "name" character varying, "description" character varying, "category_id" integer, "user_id" integer, "created_at
" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_23642321ab"
FOREIGN KEY ("category_id")
REFERENCES "categories" ("id")
, CONSTRAINT "fk_rails_541267aaf9"
FOREIGN KEY ("user_id")
REFERENCES "users" ("id")
)
In attempt to solve it, I also added this to inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'category', 'categories'
inflect.plural 'category', 'categories'
end
Which didn't help.
I also looked at few answers on stackoverflow including this, but it didn't help because I am getting this error when I run migration command.
Here is my migration files for categories and habits.
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end
class CreateHabits < ActiveRecord::Migration[5.0]
def change
create_table :habits do |t|
t.string :name
t.string :description
t.integer :user_id
t.integer :category_id
t.timestamps
end
end
end
Here is my schema.rb
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170612231416) do
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :cascade do |t|
t.text "description"
t.integer "habit_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals_habits", force: :cascade do |t|
t.integer "habit_id"
t.integer "goal_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "goals_milestones", force: :cascade do |t|
t.integer "goal_id"
t.integer "milestone_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "habits", force: :cascade do |t|
t.string "name"
t.string "description"
t.integer "user_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "milestones", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "milestones_statuses", force: :cascade do |t|
t.integer "milestone_id"
t.integer "status_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "statuses", force: :cascade do |t|
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role"
end
end
Not sure what more I am missing!
It seems there is a problem with your migrations. Run this locally to see if they run without a problem: rake db:drop && rake db:create && rake db:migrate
PS: I don't tell you to run rake db:reset because that loads the schema instead of running the migrations.
It kind of seems like your migrations files are the problem. Perhaps try rake db:schema:load instead. I have had similar problems before and it is always because of a column added after the initial migration.
Finally, I just had to destroy my Heroku app and recreate it. That solved this problem. I didn't have much data in my app. So, I could do it. If you have a really good database, I wouldn't suggest it.
To destroy app, heroku apps:destroy
And to create it again, heroku create appname

Schema.rb display Could not dump table "progresses" because of following NoMethodError # undefined method `[]' for nil:NilClass

I am trying to add a multiple image uploader with Carrierwave following the documentation to my app.
Normally my schema.rb looks like this
ActiveRecord::Schema.define(version: 20160814232416) do
create_table "progresses", force: :cascade do |t|
t.string "title"
t.string "date"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
And when I run :
rails g migration add_images_to_progresses images:json
and
rake db:migrate
My schema change and display that weird thing....
Is it a problem with Sqlite3 or Pg? What should I do ?
ActiveRecord::Schema.define(version: 20160815005123) do
# Could not dump table "progresses" because of following NoMethodError
#undefined method `[]' for nil:NilClass
end
I tried using the same code for both SQLite and PostgreSql. In SQLite there is no datatype "JSON" available, and so it throws an error. When I tried the same in PostgreSQL, it worked.
ActiveRecord::Schema.define(version: 20160815070637) do
enable_extension "plpgsql"
create_table "progresses", force: :cascade do |t|
t.string "title"
t.string "date"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.json "images"
end
end
If you want to use the SQLite then you will have to generate a string from your JSON and then save that string in your database as a regular string.
I prefer to use PostgreSQL, which supports JSON datatype.

Unable to Migrate/Seed Database - Postgres Rails

Very odd problem
I am trying to migrate my database but I keep getting:
PG::UndefinedTable: ERROR: relation "users" does not exist
Here is my migration:
class AddRoleToUser < ActiveRecord::Migration
def up
add_column :users, :role_id, :integer
end
def down
remove_column :users, :role_id
end
end
And my schema:
ActiveRecord::Schema.define(version: 20140205191602) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "horseraces", force: true do |t|
t.integer "horse_id"
t.integer "race_id"
t.datetime "entered"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "horses", force: true do |t|
t.string "name"
t.string "gender"
t.date "DOB"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "races", force: true do |t|
t.string "name"
t.integer "race_number"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "roles", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: true do |t|
t.string "first_name"
t.string "last_name"
t.string "user_name"
t.string "address"
t.string "phone_number"
t.string "email", default: ""
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "role_id"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
I have been trying:
rake db:reset
and
rake db:drop
rake db:create
rake db:migrate
And I get met with the same errors each time.
Interesting that this was working, meaning I had run these commands and got it working. I simply tried to just start fresh and now it's throwing these errors.
Worth noting - I did change a model file from users.rb to user.rb & roles.rb to role.rb I don't know if this would effect anything.
Any help would be much appreciated.
You should load the schema first then migrate:
rake db:schema:load
rale db:migrate
The first command will run your schema.rb file and create the users table. The 2nd will then run your migration and it shouldn't fail because now the users table exists.
This sort of thing often happens when you go back and edit your migrations... might not be exactly what went wrong for you - but it's the first thing that I'd suspect. My strongest suspicion would be that you somehow deleted your "create_users" migration.

Resources