Bug Report #888
ORM delete_all() does not alter the primary_key back to 1
| Status: | Closed | Start date: | ||
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | Libraries:ORM | |||
| Target version: | 2.2.1 | |||
| Resolution: | invalid | Points: |
Description
For any table with auto_increment field i.e. the primary key, delete_all() does not alter the primary key back to 1. For instance if you have 50 records in books table, the following code will delete all records BUT the next record will start from 51 NOT 1 again.
// delete all records
$book = ORM::factory("book")->delete_all();
// create a new record
$book = ORM::factory("book");
$book->title = 'My new book';
$book->save();
// output the id of this new row
echo $book->id; // will output 51
Was this intentional? If so, how can we then truncate an entire table which will them empty out all rows and the auto_increment will start from 1??
Hope it makes sense.
Thanks.
History
Updated by Woody Gilk over 4 years ago
- Status changed from New to Closed
- Resolution set to invalid
It shouldn't. That would be a Very Bad Idea (tm); what if there was a where() clause before delete_all()?! You need to manually truncate the table if that's what you want to do. The Database library does have a way to do this, as it is done different on different database systems.
Updated by shahid - over 4 years ago
- Status changed from Closed to Feedback
- Resolution deleted (
invalid)
Replying to [comment:1 Shadowhand]:
It shouldn't. That would be a Very Bad Idea (tm); what if there was a where() clause before delete_all()?! You need to manually truncate the table if that's what you want to do. The Database library does have a way to do this, as it is done different on different database systems.
Updated by shahid - over 4 years ago
OK. Point taken. Can we not have a separate method, for instance truncate(), which will only truncate the table, such that no other method can be called before truncate()??
Updated by Woody Gilk over 4 years ago
- Status changed from Feedback to Closed
- Resolution set to invalid
No.
Updated by Woody Gilk over 4 years ago
Sorry, I should expand that: No, because not every database has something comparable to TRUNCATE TABLE tablename. We can only provide functions for features that are common to all databases. Nothing prevents you from doing $db->query('TRUNCATE TABLE users'); if you want to.