3

[rails 4.1.6, ruby 2.1.3p242 ]

1.set the database.yml database: bookshop , and created the bookshop database in my database.

2.create a new table books with id int(10), name varchar(45) by SQL( I use MySQL).

3.everything is Okay, I can open the project in the browser. things became a little weird after I typed rails g scaffold Book id:integer name:string, which worked successfully. But, as I restart the rails project, then the browser came out

"Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development"

  1. bin/rake db:migrate RAILS_ENV=development it shows

== 20141003105907 CreateBooks: migrating ====================================== -- create_table(:books) rake 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./usr/local/...

Johnn
  • 105
  • 2
  • 10
  • after creating your Book table you have to migrate it to the database, so it for that it waiting for you to do the migration of the table, and the id of the table is created automatically you don't have to define it – drabo2005 Oct 03 '14 at 11:45
  • I created the tables `books` , then I use `scaffold`. so, what I've done is in a wrong sequence? – Johnn Oct 03 '14 at 12:03
  • did you try to destroy the table and recreate it without defining id ? – drabo2005 Oct 03 '14 at 12:08
  • after I drop my table 'books' manually, it works again! thank you again! :) – Johnn Oct 03 '14 at 12:10

1 Answers1

6

ID fields are added to models by default - you don't need to add them yourself.

Destroy the scaffold you made, with rails destroy scaffold Book, and create it again - this time without the id:integer field.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
  • I did so as you said. but it shows `"Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development"` again, and I typed rake.... , it shows `talbe 'books' has already exists..` – Johnn Oct 03 '14 at 12:01
  • @Johnn scaffold create migrations that rails uses. Delete your books table and run the migrations – j-dexx Oct 03 '14 at 12:08
  • alright. after I drop my table 'books' manually, it works again! thank you so much! :) – Johnn Oct 03 '14 at 12:10