Rails db migrate error - ruby-on-rails

I started to write an api in rails. I want to migrate my model to db, but I get this error:
E:\WebAuction\Backend\api>rails db:migrate
rails db:migrate
rails aborted!
StandardError: An error has occurred, all later migrations canceled:
you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:4:in `block in change'
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:3:in `change'
bin/rails:4:in `<main>'
Caused by:
ArgumentError: you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:4:in `block in change'
E:/WebAuction/Backend/api/db/migrate/20180516070242_create_wa_players.rb:3:in `change'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
== 20180516070242 CreateWaPlayers: migrating ==================================
-- create_table(:wa_players)
My Model class
class WaPlayer < ApplicationRecord::Base
has_secure_password
def change
create_table :wa_players, :id => false do |t|
t.id :String
t.playerName :String
t.uuid :String
t.password :String
t.money :String
t.itemsSold :String
t.itemsBought :String
t.earnt :String
t.spent :String
t.Permissions :String
t.Locked :String
t.timestamps null: false
end
add_index :wa_players, :id
end
end
If someone knows how to fix, please tell me how.

Don't know where you are getting your activerecord knowledge from, but you seem to have messed it up. It's not a t.<column> <type>. It is t.<type> <column>.
t.string :id
t.string :player_name
And so on.
(Note: most names in ruby follow snake_case naming convention, not camelCase).

From the content provided, it seems like you are heading the wrong way:
Firstly, generating migration for model which seems you have done it as you have shared the model class.
Model Class
class WaPlayer < ApplicationRecord::Base
has_secure_password
end
Thus, there would be migration for the same ie:
def change
create_table :wa_players, :id => false do |t|
t.id :String
t.playerName :String
t.uuid :String
t.password :String
t.money :String
t.itemsSold :String
t.itemsBought :String
t.earnt :String
t.spent :String
t.Permissions :String
t.Locked :String
t.timestamps null: false
end
add_index :wa_players, :id
end
The contents of the migration are wrong which needs to be updated with:
def change
create_table :wa_players, :id => false do |t|
t.string :id
t.string :player_name
t.string :uuid
t.string :password
t.string :money
t.string :items_sold
t.string :items_bought
t.string :earnt
t.string :spent
t.string :permissions
t.string :locked
t.timestamps null: false
end
add_index :wa_players, :id
end
Now you need to run
rake db:create
if the db has not been migrated else run
rake db:migrate.

It will be t.string then column_name like
def change
create_table :wa_players, :id => false do |t|
t.string :id
t.string :player_name
t.string :uuid
t.string :password
t.string :money
t.string :items_sold
t.string :items_bought
t.string :earnt
t.string :spent
t.string :permissions
t.string :locked
t.timestamps null: false
end
add_index :wa_players, :id, unique: true
end
Look at that Creating a Table
Update
I don't know how you trying but follow these steps
run your console like rails g model WaPlayer
Go to db/migrate/TIMESTAMP_create_wa_players.rb
Then paste this change method into class
Then run rails db:migrate or rake db:migrate after rake db:create if you did not create the DB till now

Related

Try to add new column into Posgres DB but only success when there is no data in table

I'm trying to add new column into the table by 'rake db:migrate',but it return nothing in cmd.Then i try 'rake db:migrate:status' this time it return the following...
C:\Sites\seas>rake db:migrate:status
database: seas_development
Status Migration ID Migration Name
--------------------------------------------------
up 20160323084854 Create equipment
up 20160329072332 Devise create users
Below is inside my migration file...
class CreateEquipment < ActiveRecord::Migration
def change
create_table :equipment do |t|
t.string :name
t.string :equip_id
t.date :buy_date
t.string :brand
t.string :note
t.date :exp
t.string :status
t.string :serial
t.float :price
t.string :pic_id
t.string :ownby
t.timestamps null: false
end
add_column :equipment, :process ,:string
end
end
This only happen if there exist some data in the table,otherwise migration work fine.
Any suggestion ?
You have a typo
add_column :equipment, :process ,:string
Table name should be in plural
add_column :equipments, :process ,:string
But... if the migration had already be ran, then it will not run again. Create a new migration
rails g migration add_process_to_equipments process
rake db:migrate
Ta dah!

can anyone explain me this error?

I am getting this while i use Postgres.
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "retailers" does not exist
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830"
FOREIGN KEY ("retailer_id")
REFERENCES "retailers" ("id")
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "retailers" does not exist
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830"
FOREIGN KEY ("retailer_id")
REFERENCES "retailers" ("id")
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
PG::UndefinedTable: ERROR: relation "retailers" does not exist
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Here are my stations and retailers model.
Retailer
class Retailer < User
has_many :stations
has_many :retailer_suppliers
has_many :suppliers , through: :retailer_suppliers, as: :connections
end
Stations
class Station < ActiveRecord::Base
belongs_to :retailer
has_many :tanks
end
Here is my Station Migration
class CreateStations < ActiveRecord::Migration
def change
create_table :stations do |t|
t.string :brand
t.string :business_name
t.string :tax_id
t.string :phone_number
t.string :contact_person
t.string :cell_number
t.string :address1
t.string :address2
t.string :city
t.string :state
t.string :zip
t.string :station_reg_number
t.references :retailer, index: true, foreign_key: true
t.timestamps null: false
end
end
end
I dont have retailer migration because its inheriting from the User. Here is my User migration
User
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
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
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
Add Type to User
class AddTypeToUsers < ActiveRecord::Migration
def change
add_column :users, :type, :string
end
end
Add extra attributes to user
class AddExtraToUsers < ActiveRecord::Migration
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :phone_number, :string
add_column :users, :cell_number, :string
add_column :users, :tax_id, :string
add_column :users, :business_name, :string
add_column :users, :address1, :string
add_column :users, :address2, :string
add_column :users, :city, :string
add_column :users, :state, :string
add_column :users, :zip_code, :string
add_column :users, :years_in_business, :string
end
end
And add account number to user
class AddAccountNumberToUsers < ActiveRecord::Migration
def change
add_column :users, :account_number, :string
add_index :users, :account_number
end
end
It works with no errors when i use Sqlite3 but i get errors in Production heroku using postgres. so i decided to use postgres in developemnt and i saw the above errors which i cannot understand. Thank you in advance
PG::UndefinedTable: ERROR: relation "retailers" does not exist
This error simply means that the retailers table is not present in your database when you try to reference this in another place. All you need is to make sure you create retailers table before you try to use/reference it in some some migration.
The error is coming from this line in your migration:
t.references :retailer, index: true, foreign_key: true
The foreign key option means that rails is trying to create a foreign key between retailer_id on the stations table and id on the non existant retailers table.
Although migrations are frequently created at the same time as a model, they're not really connected - the migration doesn't know that retailer is an STI model.
As far as I can tell you will need to remove the foreign key option from the call to references and add the foreign separately with add_foreign_key:
add_foreign_key :stations, :users, column: "retailer_id"
You probably didn't encounter this in development because earlier versions of sqlite3 didn't support foreign keys and current versions require that it be activated (see docs): unless activated it just ignores foreign key constraints (as long as they are syntactically correct)

ActiveRecord::UnknownAttributeError: unknown attribute: p_id

after I run
bundle exec rake db:seed
The titled error will show.
Could anyone has a look at my RoR code? I've not sure where is the issue.
my
product.rb
file
class Product < ActiveRecord::Base
set_primary_key :p_id
attr_accessible :p_id, :name, :category, :description, :price
end
schema.rb
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20111119180638) do
create_table "products", :force => true do |t|
t.string "p_id"
t.string "name"
t.string "category"
t.text "description"
t.string "price"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
create_products.rb
class CreateProducts < ActiveRecord::Migration
def up
create_table :products, {:id => false} do |t|
t.string :p_id
t.string :name
t.string :category
t.text :description
t.string :price
# Add fields that let Rails automatically keep track
# of when products are added or modified:
t.timestamps
end
execute "ALTER TABLE products ADD PRIMARY KEY (p_id);"
end
def down
drop_table :products
end
end
primery_key method is expecting an string:
self.primary_key 'p_id'
http://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html#method-i-primary_key-3D

Rails 4 SQLite3::SQLException error

I have a devise User model which I want to add a admin boolean field so I ran
rails generate migration add_admin_to_users admin:boolean which created the following migration
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean
end
end
However when I run rake db:migrate I keep getting the following error
SQLite3::SQLException: no such table: users: ALTER TABLE "users" ADD "admin" boolean/home/notebook/.rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
I have tried to rake db:migrate VERSION=0 to rollback to the beginning and redone rake db:migrate again but I keep getting the same error
Here is my user model migration file
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
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
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
add_index :users, :confirmation_token, :unique => true
# add_index :users, :unlock_token, :unique => true
end
end
My db/schema.rb file is as follows:
ActiveRecord::Schema.define(version: 20140217093954) do
create_table "entities", force: true do |t|
t.string "entity"
t.string "genre"
t.string "url"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "entities", ["entity"], name: "index_entities_on_entity", unique: true
end
It sounds like your development database is not in sync with the schema (or what you think the schema is). Running database migrations is intended for incremental changes and not intended to be be a way to rollback the database and run from the first migration. For this you want to use rake db:reset, which will drop the database and load the schema from db/schema. This post has a good overview of the different database rake commands.
Your database has not users table, you need to create users table first:
rails g migration create_users
And put that migration before the one you are trying to run now, i.e. changing its timestamp.
An easier option is to add in your current file:
class AddAdminToUsers < ActiveRecord::Migration
def change
create_table :users
add_column :users, :admin, :boolean
end
end
Whatever of the solutions you apply, you have a problem with the order of your migrations, search inside migrate folder for a migration that actually creates users table.

Index name is too long - Rails 3

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.

Resources