Ticket #888 (closed Patch: invalid)

Opened 6 weeks ago

Last modified 6 weeks ago

ORM delete_all() does not alter the primary_key back to 1

Reported by: shahid Owned by: Shadowhand
Priority: minor Milestone: 2.2.1
Component: Libraries:ORM Version: 2.2 Release
Keywords: ORM delete_all truncate auto_increment Cc:

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.

Change History

follow-up: ↓ 2   Changed 6 weeks ago by Shadowhand

  • 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.

in reply to: ↑ 1   Changed 6 weeks ago by shahid

  • status changed from closed to reopened
  • resolution deleted

Replying to 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.

  Changed 6 weeks ago by shahid

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()??

  Changed 6 weeks ago by Shadowhand

  • status changed from reopened to closed
  • resolution set to invalid

No.

  Changed 6 weeks ago by Shadowhand

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.

Note: See TracTickets for help on using tickets.