Opened 4 years ago

Closed 4 years ago

#16427 closed New feature (wontfix)

truncate table method in ORM

Reported by: adamnelson Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In the interest of having a more complete ORM, there should be a truncate table command available. This could be used in specialized cases where the user needs to delete very large tables (i.e. migrations on volatile tables, tables with data that gets purged regularly, etc...)

Change History (3)

comment:1 Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

As far as I can tell, TRUNCATE is not part of SQL-92, and it isn't implemented by sqlite, so it isn't a good candidate for ORM support.

What's wrong with MyModel.objects.delete()? At least sqlite will optimize this by performing a truncate. I don't know very well the internal of other database engines, but they may optimize it too.

Finally, you can use raw SQL.

I'm leaning towards "wontfix", but let's wait for a core developer's opinion,

comment:2 Changed 4 years ago by adamnelson

The solution to the sqlite issue is simply to do "DELETE table_name" for that backend when calling the truncate() method.

Sybase claims that TRUNCATE is SQL-92 compliant as part of the Transact-SQL extension.

The problem with the delete() method is that it's exceedingly slow. On tables with more than 1M records, DELETE can take up to 10 minutes to run compared to seconds to truncate - with the same outcome.

comment:3 Changed 4 years ago by lukeplant

  • Resolution set to wontfix
  • Status changed from new to closed

wontfixing, for the reasons aaugustin gave. If delete() is too slow use manual SQL truncate. . For specialised needs, this is good enough, and it's not hard to get the db table name for a model.

Note: See TracTickets for help on using tickets.
Back to Top