migrate is not running - ruby-on-rails

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

Related

PG::UndefinedColumn: ERROR: column "image_file_name" of relation "articles" does not exist

Can someone please help me with runing heroku run db:migrate? I've forgot to run db:migration on heroku along with migration on dev env. I made coulple of them and now I'm receiving the below error:
wozane:~/workspace (master) $ heroku run rake db:migrate
Running rake db:migrate on ⬢ wozane... up, run.7786
(0.8ms) SELECT pg_try_advisory_lock(96974639112725850);
ActiveRecord::SchemaMigration Load (1.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to RemoveColumnImage (20160917131520)
(0.7ms) BEGIN
== 20160917131520 RemoveColumnImage: migrating ================================
-- remove_column(:articles, :image_file_name, :string)
(1.5ms) ALTER TABLE "articles" DROP "image_file_name"
(0.7ms) ROLLBACK
(0.8ms) SELECT pg_advisory_unlock(96974639112725850)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedColumn: ERROR: column "image_file_name" of relation "articles" does not exist
: ALTER TABLE "articles" DROP "image_file_name"
The case is that this column has been deleted and it doesn't exist.
Migration mentioned in the error message (number 20160917131520):
class RemoveColumnImage < ActiveRecord::Migration[5.0]
def change
remove_column :articles, :image_file_name , :string
remove_column :articles, :image_content_type, :string
remove_column :articles, :image_file_size, :integer
remove_column :articles, :image_updated_at, :datetime
end
end
Schema.rb looks like that:
ActiveRecord::Schema.define(version: 20160921115118) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "text"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "img_url"
end
create_table "photos", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "title"
t.string "img_url"
t.text "text"
end
end
I tried to #the whole code in migration and it didn't help along with those comments which did not work:
rails db:environment:set RAILS_ENV=production
heroku run rake db:reset
heroku run rake db:migrate -app wozane
heroku pg:reset DATABASE --confirm wozane
Does anyone have an idea how to run the heroku migration in my case?
Thanks in advance.
just comment out code of remove column migration.
class RemoveColumnImage < ActiveRecord::Migration[5.0]
def change
#remove_column :articles, :image_file_name , :string
#remove_column :articles, :image_content_type, :string
#remove_column :articles, :image_file_size, :integer
#remove_column :articles, :image_updated_at, :datetime
end
end
And try to run heroku run rake db:migrate -app wozane

rake db:migrate Error 1064 Lynda.com Ruby on Rails 4 Issue

class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end
def down
drop_table :users
end
end
I'm attempting to run rake db:migration and I'm getting
mysql> rake db:migration;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rake db:migration' at line 1
t.string "email", :default => "" :null => false
You're missing a comma between your arguments to string, e.g.,
t.string "email", default: "", null: false
You should not run the rake command from mysql console.
Open your terminal and run rake db:migrate from there. If you still face any issues - update the question.

Migration runs but table is not created

I am running the following migration:
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
# t.datetime "created_at"
# t.datetime "updated_at"
t.timestamps
end
end
def down
drop_table :users
end
end
But after running rake db:migrate, even though the log shows this:
== 20150705121953 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0013s
== 20150705121953 CreateUsers: migrated (0.0013s) =============================
When I go to the mysql console and run SHOW TABLES; it displays:
mysql> USE my_db;
Database changed
mysql> SHOW TABLES;
Empty set (0.00 sec)
If I have the migration create the users table, why does the database not show a single table?
This may seem silly but do you have MySQL imported into the rails application? I believe it automatically is setup for SQLite3. I had this issue when I began and have since removed it. It should show this information in the database.yml file.
Joe

How to remove the migration in my database?

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.

Heroku - PGError: ERROR: syntax error at or near "ENGINE"

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.

Resources