I am trying to run
heroku rake db:migrate
to run my migrations on heroku and the first two migrations ran great but the third which looks like this
create_table :charities, :options => "ENGINE=MyISAM" do |t|
t.string :name, :null => false
t.string :title, :null => false
t.timestamps
end
add_index :charities, :name
add_index :charities, :title
Migrating to CreateCharitiesAndThemes (20091019140537)
== CreateCharitiesAndThemes: migrating =======================================
-- create_table(:charities, {:options=>"ENGINE=MyISAM"})
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: syntax error at or near "ENGINE"
LINE 1: ..., "created_at" timestamp, "updated_at" timestamp) ENGINE=MyI...
^
: CREATE TABLE "charities" ("id" serial primary key, "name" character varying(255) NOT NULL, "title" character varying(255) NOT NULL, "created_at" timestamp, "updated_at" timestamp) ENGINE=MyISAM
Heroku uses PostgreSQL, and MyISAM engine is MySQL-specific. I suggest you remove that part. Or, add checking on what database is used and make that optional.
Here's a link to how to check the database.
Related
I made a model called user and in the timestamp_create_users.rb ( where timestamp = long number ) I have the following:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.integer :userNum, :limit => 7
t.string :username, :limit => 32
t.string :fname, :limit => 40
t.string :surname, :limit => 40
t.string :email, :limit => 50
t.string :kNum, :limit => 8
t.string :password, :limit => 80
t.boolean :isTeacher, :default => false
t.timestamps null: false
end
end
end
The problem is that it gives me a long error message and here the head of it:
rake db:migrate
== 20171126181930 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "userNum" integer(7), "username" varchar(32), "fname" varchar(40), "surname" varchar(40), "email" varchar(50), "kNum" varchar(8), "password" varchar(80), "isTeacher" boolean DEFAULT 'f', "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
and the user.rb ( inside the model ) is empty. What am I doing wrong ?
Thanks for your time
Looks like your Sqlite DB is already having a users table. If this is a development machine you should try
rake db:drop
rake db:create
rake db:migrate
Run rake db:setup
,it basically runs all of the following commands, it will resolve your error and setup your application in a single command
rake db:drop
rake db:create
rake db:migrate
rake db:seed
So.. im trying to migrate my db to heroku for the first time, doing:
heroku rake db:migrate
but im getting this horrible error that i cant resolve !
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
ActiveRecord::SchemaMigration Load (1.8ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to CreateComments (20150528110329)
(1.6ms) BEGIN
== 20150528110329 CreateComments: migrating ===================================
-- create_table(:comments)
(9.4ms) CREATE TABLE "comments" ("id" serial primary key, "name" character varying, "body" character varying, "post_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
(3.8ms) CREATE INDEX "index_comments_on_post_id" ON "comments" ("post_id")
(6.4ms) ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_2fd19c0db7"
FOREIGN KEY ("post_id")
REFERENCES "posts" ("id")
PG::UndefinedTable: ERROR: relation "posts" does not exist
: ALTER TABLE "comments" ADD CONSTRAINT "fk_rails_2fd19c0db7"
FOREIGN KEY ("post_id")
REFERENCES "posts" ("id")
(1.6ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
what i can understand is that its trying to reference from a table that doesnt exist yet.. but I dont know how to create my post table first...
schema.rb>
ActiveRecord::Schema.define(version: 20150622053408) do
create_table "comments", force: :cascade do |t|
t.string "name"
t.string "body"
t.integer "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "subtitle"
t.text "content"
t.string "img"
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context", limit: 128
t.datetime "created_at"
end
add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", force: :cascade do |t|
t.string "name"
t.integer "taggings_count", default: 0
end
add_index "tags", ["name"], name: "index_tags_on_name", unique: true
end
I tried switching in every way possible my code but with no results.
Hope that someone could help me out... thanks for everything!
I'm of the opinion that somewhere along the line one of your migrations were edited (which would be the problem if not properly rolledback first etc) and is now causing a major problem. And since you need to keep the data and can't do a full db:rollback I would say there is maybe one option I can think of unless someone else has a better idea. I never recommend editing migration files but I've had to do it in the past in certain circumstances. Do the following and make sure you keep in mind that any changes to the db can't be undone with a simple
git checkout .
I would create a new migration file by hand, not by using the rails generator and give it a timestamp that is a couple seconds before the timestamp of the Comments table migration file. I would then create the Posts table in that migration and edit the other existing migration that created the Posts table to just add it's columns. You must then go into your database (Not your rails console but your database which is probably Postgres) and insert this migration into the "schema_migrations" table. This schema_migrations table is automagically setup by rails whenever you generate a migration and run db:migrate. You'll notice the entries in this schema_migrations table are listed by "Version" where the version is the timestamp of the migration in your migration folder. You must then add the migration file timestamp to your schema_migrations table and it should be good to go.
UPDATE: IF you don't care about losing the data you can rollback all migrations, edit your migrations files and then re-run them.
bundle exec rake db:migrate:status #Look at the first version id and copy it.
bundle exec rake db:rollback VERSION=theVersionNumberYouCopiedHere
Then re check the migration statuses with the first command and make sure all migration statuses are listed as "down". This will have dropped all tables etc. Then go into your migration files and remove the referencing of your posts table from the Comments table that was giving you the issues. Then re-run all migrations again. Then add your posts_id reference to the comments table as follows by generating and running the following migration:
rails g migration AddPostToComments post:references
Looking in that migration file you should see something like this:
class AddPostToCommentss < ActiveRecord::Migration
def change
add_reference :comments, :post, index: true
add_foreign_key :comments, :posts
end
end
Now run bundle exec rake db:migrate and you should be good to go. Commit all the changes to git and then push to heroku!
I have been trying to use devise to make an authentication system for a blog that I am programming.When I try to view the blog, it says (Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.), and when I run the command, it fails and renders this message:
Moussas-MacBook-Pro:theBlog moussasarr$ bin/rake db:migrate RAILS_ENV=development
== 20141031151735 SorceryCore: migrating ======================================
-- create_table(:authors)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "authors" already exists: CREATE TABLE "authors" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(255) NOT NULL, "email" varchar(255) NOT NULL, "crypted_password" varchar(255) NOT NULL, "salt" varchar(255) NOT NULL, "created_at" datetime, "updated_at" datetime) /Users/moussasarr/.rvm/gems/ruby-2.0.0-p576#railstutorial_rails_4_0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
. I did migrate the authors table in the past but I had to restart from an earlier point as it was not working out. How can I delete the SorceryCore migration to make a new one ?
Here is the migration table:
class SorceryCore < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :username, :null => false
t.string :email, :null => false
t.string :crypted_password, :null => false
t.string :salt, :null => false
t.timestamps
end
add_index :authors, :email, unique: true
end
end
I really want to get this table out of the database and push in a new table.
Thanks a lot for your help ! This is the original problem that made me restart at an earlier level and made me lose so much time.
You can do db:migrate:down VERSION=your_migration_version to drop the table from the database, then delete the migration file and then run the migrations that devise needs for your authentication.
However, make sure that you don't need the authors table in any migrations until the devise one as it won't exist and can be a potentially big problem with your codebase and everything.
I am trying to setup a Rails app on Heroku using PG, and also using devise and oauth to interact with the Yahoo Sports api.
I am running into an issue where when the users goes to authenticate the Heroku logs are telling me:
ActiveRecord::StatementInvalid (PG::Error: ERROR: value too long for type character varying(255)
So after reading this Stack Overflow post PostgreSQL string(255) limit - Rails, Ruby and Heroku, I tried changing the token to text from string since that seemed to be the longest thing that may be causing the over 255 limit. My migration looked like this:
class AddAccessTokenToAuthentications < ActiveRecord::Migration
def change
add_column :authentications, :token, :text, :limit => nil
add_column :authentications, :secret, :string
end
end
However for some reason it seems to be changing everything to text. When I look at my schema after migrating it shows:
create_table "authentications", :force => true do |t|
t.integer "user_id"
t.text "provider", :limit => 255
t.text "uid", :limit => 255
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "token", :limit => 255
t.text "secret", :limit => 255
end
When I push to Heroku and try to migrate on Heroku I am getting this error:
-- create_table("authentications", {:force=>true})
NOTICE: CREATE TABLE will create implicit sequence "authentications_id_seq" for serial column "authentications.id"
rake aborted!
PG::Error: ERROR: type modifier is not allowed for type "text"
LINE 1: ...serial primary key, "user_id" integer, "provider" text(255),...
: CREATE TABLE "authentications" ("id" serial primary key, "user_id" integer, "provider" text(255), "uid" text(255), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "token" text(255), "secret" text(255))
I am not sure how to resolve this or what I should be looking into.
PG::Error: ERROR: type modifier is not allowed for type "text"
means you can't use :limit
What you can do is either:
t.text "provider" # = varchar(max)
or
t.string "provider", :limit => 2147483647 # for max limit based on your db
Your text fields are unable to have a limit on them. Perhaps you meant to use string instead?
I am trying to run this migration:
class RemoveClientFromSalesteam < ActiveRecord::Migration
change_table :sales_teams do |t|
t.remove :client_id
end
end
This is the error I am getting:
rake db:migrate
-- change_table(:sales_teams)
rake aborted!
An error has occurred, this and all later migrations canceled:
Index name 'temp_index_altered_sales_teams_on_client_priority_and_personal_priority' on table 'altered_sales_teams' is too long; the limit is 64 characters
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
This is what my schema.rb looks like:
create_table "sales_teams", :force => true do |t|
t.string "name"
t.integer "firm_id"
t.boolean "client_priority"
t.boolean "personal_priority"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "client_id"
end
add_index "sales_teams", ["client_id"], :name => "index_sales_teams_on_client_id"
add_index "sales_teams", ["client_priority", "personal_priority"], :name => "index_sales_teams_on_client_priority_and_personal_priority"
add_index "sales_teams", ["name", "firm_id"], :name => "index_sales_teams_on_name_and_firm_id"
Thoughts?
Thanks.
Drop the index, remove your column, and then re-add the index:
def up
remove_index :sales_teams, :column => [ :client_priority, :personal_priority ]
remove_column :sales_teams, :client_id
add_index :sales_teams, [ :client_priority, :personal_priority ]
end
I'm guessing that you're using SQLite, most databases support real ALTER TABLE operations for removing columns but SQLite forces you to copy the table (and indexes), drop the table, and copy everything back; the Rails SQLite driver takes care of this behind the scenes but, apparently, doesn't know about the identifier length limit.
You can also specify your own index names by using the :name option to add_index and remove_index if necessary.