0

I created a model called customer. The migrate file is as follows:-

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
        create_table :customers , :primary_key => :customer_id , do |t|
          t.integer :customer_id
          t.string :first_name
          t.string :last_name
          t.string :address_1
          t.string :address_2
          t.string :city
          t.string :state
          t.bigint :postal_code

          t.timestamps
        end
  end
end

Now when I run rails db:migrate, this is the error that I get:-

rails aborted!
SyntaxError: /home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3: syntax error, unexpected keyword_do_block
imary_key => :customer_id , do |t|
                              ^
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4: syntax error, unexpected tSYMBEG, expecting keyword_end
          t.integer :customer_id
                     ^
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:16: syntax error, unexpected keyword_end, expecting end-of-input
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

The schema.rb file has no sign of the customer table either. Can someone help? EDIT: After removing the comma after customer_id I'm getting these:-

== 20170428100848 CreateCustomers: migrating ==================================
-- create_table(:customers, {:primary_key=>:customer_id})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

you can't redefine the primary key column 'customer_id'. To define a custom primary key, pass { id: false } to create_table.
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4:in `block in change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3:in `change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
ArgumentError: you can't redefine the primary key column 'customer_id'. To define a custom primary key, pass { id: false } to create_table.
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:4:in `block in change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/db/migrate/20170428100848_create_customers.rb:3:in `change'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `require'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/rails:9:in `<top (required)>'
/home/gauri/Academics/CS/Ruby on Rails/ScholarShip/ScholarShip/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
1
  • I have added the steps to change primary key for a model Commented Apr 28, 2017 at 10:32

4 Answers 4

1

Remove , from below line:

create_table :customers , :primary_key => :customer_id , do |t|

correct is:

create_table :customers , :primary_key => :customer_id do |t|

Code to set primary key:

 create_table(:my_table, :primary_key => 'userID') do |t|
   # Primary key column will be created automatically
   # Do not create here
   # t.column :userID, :integer, :null => false
   ...
 end
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting some other error. I've pasted the code after the 'EDIT' portion
@sindhugauri do rake db:rollback then run rake db:migrate
@sindhugauri check my updated post to set primary key
@sindhugauri accept my answer if it works for you. Thanks
have you done this: create_table :customers, id: false do |t|
0

Remove the last comma on the third line:

create_table :customers , :primary_key => :customer_id do |t|

Comments

0

You have an extra , at the end of line:3

Change this line

create_table :customers , :primary_key => :customer_id , do |t|

to

create_table :customers , :primary_key => :customer_id do |t|

You can check for the syntax and other options here

EDIT

For the second error remove the :primary_key => :customer_id from migration

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
    create_table :customers, id: false do |t|
      t.integer :customer_id
      t.string :first_name
      t.string :last_name
      t.string :address_1
      t.string :address_2
      t.string :city
      t.string :state
      t.bigint :postal_code

      t.timestamps
    end
  end
end

and specify primary key in model

class Customer
  self.primary_key = :customer_id
end

1 Comment

It says SQLite3::SQLException: table "customers" already exists: CREATE TABLE "customers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "customer_id" integer, "first_name" varchar, "last_name" varchar, "address_1" varchar, "address_2" varchar, "city" varchar, "state" varchar, "postal_code" bigint, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
0

Try the below

class CreateCustomers < ActiveRecord::Migration[5.0]
  def change
        create_table :customers , :id => :false do |t|
          t.integer :customer_id, primary: true
          t.string :first_name
          t.string :last_name
          t.string :address_1
          t.string :address_2
          t.string :city
          t.string :state
          t.bigint :postal_code

          t.timestamps
        end
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.