Domanda

I'm new to rails so maybe the record id is not updated on destroy.

I used rails scaffold to generate my MVC for a products page. When I add an entry to my data base through my products/new page it adds an id number to the entry. When in a browser if I rollover the show/edit/destroy links on my index view it will give me a url link of localhost:3000/products/1. Now if I remove the entry by using the destroy link it removes the entry correctly, but now when I add another entry it shows the id as 2 not 1. Why does it remove the entry from the database but not reset the id back to 1.

This is for the build in server in rails app sqlite3.

È stato utile?

Soluzione 3

you would have to rake db:drop your database (which deletes it) and then rake db:migrate to create your tables again.

Altri suggerimenti

Sqlite is using a sequence to determine the next available integer to use for the primary id. Sequences don't "go back" even if you destroy the record with the latest one. They keep incrementing. Some databases will restart at zero once they reach a maximum, some will error.

Some databases tie a sequence to the table. However in some, sequences are their own thing. You could (if you wanted to) use a sequence to get unique ID's for several different tables.

Check sqlite's documentation for details...

The command rake db:schema:dump Dumps the current environment’s schema to db/schema.rb. To reset it back or delete all and migrate it again you should use the command rake db:setup Which runs rake db:schema:load and rake db:seed.

Some of the other important commands are;

rake db:schema:load This command loads the schema into the current environment’s database.

rake db:schema:dump This command dumps the current environment’s schema to db/schema.rb.

rake db:setup This command runs rake db:schema:load and rake db:seed.

rake db:reset This command runs rake db:drop and rake db:setup.

rake db:migrate:reset This command runs rake db:drop then rake db:create and rake db:migrate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top