I've rails app that has some migrations. When I hit rake db:migrate:status to see what status is set, all except ********** NO FILE ********** were down. But the migrations have already made, so there seems no problem about model side. Here are files may help to explain:
Output of rake db:migrate:status:
up 20130727003912 ********** NO FILE **********
down 20130728000151 Devise create users
down 20130728000335 Create friends
down 20130728000346 Create addresses
down 20130728000356 Create authies
down 20130728000413 Add indexes
At this time, rake db:migrate output:
== DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq1" for serial column "users.id"
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "users" already exists
: CREATE TABLE "users" ("id" serial primary key, "username" character varying(255), "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, "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) /home/ekrem/workspace/contactman/db/migrate/20130728000151_devise_create_users.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
db/schema.rb:
ActiveRecord::Schema.define(:version => 20130727003912) do
create_table "addresses", :force => true do |t|
t.string "title"
t.text "address"
t.string "phone"
t.string "city"
t.integer "friend_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.string "country"
end
add_index "addresses", ["friend_id"], :name => "index_addresses_on_friend_id"
add_index "addresses", ["slug"], :name => "index_addresses_on_slug", :unique => true
create_table "authies", :force => true do |t|
t.string "provider"
t.string "uid"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "authies", ["user_id"], :name => "index_authies_on_user_id"
create_table "friends", :force => true do |t|
t.string "name"
t.string "surname"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "slug"
t.string "imported_file_name"
t.string "imported_content_type"
t.integer "imported_file_size"
t.datetime "imported_updated_at"
end
add_index "friends", ["slug"], :name => "index_friends_on_slug", :unique => true
add_index "friends", ["user_id"], :name => "index_friends_on_user_id"
create_table "users", :force => true 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
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 "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
How can I fix this issue to make all migrations seem up?
Your users tables is already created in your database but your schema migration is not updated with latest migration. I dont no why it has not been updated. You need to fix it then you can do it in two ways 1. drop the database and create one more or 2. run the down migration then up migration
rake db:drop
rake db:create
rake db:migrate
You can set force to true:
create_table :users, force: true do
...
end
in your migration and delete the migration key in your "schema_migrations" table by hand and rerun the migration.
this worked perfectly for me:
rails db:drop
rails db:create
rails db:migrate
You will notice that the file developmen.sqlite3 was removed and created again and now everything will be up and running rails db:migrate:status
Related
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
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.
Why Why ActiveRecord::StatementInvalid: SQLite3::SQLException: near ")": syntax error: INSERT INTO "user_friendships" () VALUES ()
When trying to test:
$ ruby -I test test/unit/user_friendships_test.rb
require 'test_helper'
class UserFriendshipsTest < ActiveSupport::TestCase
should belong_to (:user)
should belong_to (:friend)
test "that creating a frinedship works without raising an exception" do
assert_nothing_raised do
UserFriendship.create user: users(:a), friend: friends(:b)
end
end
end
Any idea?
UPDATE: his is part of the Schema.rb
create_table "user_friendships", :force => true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_friendships", ["user_id", "friend_id"], :name => "index_user_friendships_on_user_id_and_friend_id"
create_table "users", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.string "profile_name"
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
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
I ran into this as well. It seems to be related to screwing up the rails naming conventions. In my case, doing "rails g model activity_item" instead of "rails g model activity_items" solved the issue.
I'm trying to play around with different messaging gems that all use devise. Therefore, after installing Devise on master branch of git, I checked out a new branch to try one gem "rails messaging" https://github.com/frodefi/rails-messaging, and when I couldn't get it to work, I committed the changes to that branch, and then I went back to master and checked out a new branch to try another gem "mailboxer" https://github.com/ging/mailboxer/tree/master/lib. In the gemfile of this new branch there was no trace of the gems from the first branch, however when I tried rake db:migrate for this second branch after installing the gems, I got this error message, which seemed to suggest that a table from the first branch was interfering with the rake of the second branch, unless by doing
rails g mailboxer:install
It automatically runs the rake db:migrate. However, the README says that I have to run rake db:migrate, so I'm not sure...
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "conversations" already exists: CREATE TABLE "conversations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject" varchar(255) DEFAULT '', "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL
)
Can anyone suggest how I can work around this? I don't really know what commands to run to figure this out.
This is the scheme.rb file. Based on the index "messaging users" i'm guessing it's the setup created by the rails messaging gem (on the first branch) but I'm not totally sure. I looked at the source code on git but got kinda lost because I'm not very experienced.
ActiveRecord::Schema.define(:version => 20120302041333) do
create_table "conversations", :force => true do |t|
t.string "subject", :default => ""
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "messaging_users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :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
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"
end
add_index "messaging_users", ["email"], :name => "index_messaging_users_on_email", :unique => true
add_index "messaging_users", ["reset_password_token"], :name => "index_messaging_users_on_reset_password_token", :unique => true
create_table "notifications", :force => true do |t|
t.string "type"
t.text "body"
t.string "subject", :default => ""
t.integer "sender_id"
t.string "sender_type"
t.integer "conversation_id"
t.boolean "draft", :default => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.integer "notified_object_id"
t.string "notified_object_type"
t.string "notification_code"
end
add_index "notifications", ["conversation_id"], :name => "index_notifications_on_conversation_id"
create_table "receipts", :force => true do |t|
t.integer "receiver_id"
t.string "receiver_type"
t.integer "notification_id", :null => false
t.boolean "read", :default => false
t.boolean "trashed", :default => false
t.boolean "deleted", :default => false
t.string "mailbox_type", :limit => 25
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "receipts", ["notification_id"], :name => "index_receipts_on_notification_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :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
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.string "name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
The issue here is that while your code, schemas, gems, and migrations are stored in git, the database itself is not. So when you switch between branches, the database still retains its state.
These solutions will all work:
drop and re-seed your database
rake db:reset
migrate down (while on the old branch) to undo the migration. I'm not sure if the gem installed a migration in db/migrate or ran a migration directly from the gem, so this isn't as easy, but is better for data integrity if you don't want to wipe your data.
reload the schema you have stored in git (this has the same effects as the first one)
rake db:schema:load
use an sqlite database for now - while not appropriate for production, if you check in the database to git, it will behave as you're wishing your current database behaved. I should warn you though that large files like this will be a pain. Better to use one of the other approaches.
Yeah switching branches with different migration levels is a mess. There is no 'one-liner' to do this properly. There are four things that can be out of sync. You'll need to:
1) Determine what migrations your branch has:
(look at the contents of db/migrations)
2) Determine what your app thinks your database looks like:
(look at db/schema.rb)
3) Determine what migrations your database thinks it has by looking at the schema_migrations table:
~> rails db console
(opens up your db console, in my case mysql...)
mysql> SELECT * FROM schema_migrations; # don't forget the ';' at the end!
(outputs big huge list of migration timestamps)
4) Look at the actual tables to see if the migrations have been applied.
mysql> describe table_name;
(outputs the column names, see if they have been updated per the migration)
5) Now the part that requires thinking. There are a couple possible scenarios. I can only speak to the one I ran into:
Your migration files (1), your schema.rb(2) and your actual tables (4) match, but your schema_migrations table (3) is missing the migration timestamp. This was my issue.
If you don't care about the data: rake db:reset. It will trash your whole database and recreate the structure from scratch
Please suggest other scenarios with solutions..
I've created a one-to-one association between my Admin and Report models and it isn't working just yet. I'm using Devise to log in via an Admin model so in the controller I'm using the current_admin helper. Silly question, but what migration do I need to run to get this working?
Error
ActiveRecord::StatementInvalid (PGError: ERROR: column reports.admin_id does not exist
2011-10-14T09:16:57+00:00 app[web.1]: LINE 1: SELECT "reports".* FROM "reports" WHERE ("reports".admin_id = ...
Report model
belongs_to :admin, :foreign_key => "admin_id"
Admin model
has_one :report, :foreign_key => "admin_id"
Controller
#report = current_admin.report
Schema
create_table "reports", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "admins", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :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
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"
end
You need admin_id in reports. You can remove the foreign_key stuff in your models. That's done automatically. Create a migration, add
add_column :reports, :admin_id, :integer
Run rake db:migrate and you're done.