I have been using orm for my app, and try not to use sql to avoid sql syntax error when changing between different dbms.
At the moment I am testing with sqlite and mysql. I figured out to delete a table we can use:
db.session.query(models.MyTable).delete()
Unfortunately this method does not really "truncate" the table, because It does not reset the counting of autoincrement to 0.
Is there anyway to do the real truncate in SqlAlchemy orm ?
If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.
Just call drop() against the table object.
sqlite> DELETE FROM table_name; Following is the basic syntax of DROP TABLE. sqlite> DROP TABLE table_name; If you are using DELETE TABLE command to delete all the records, it is recommended to use VACUUM command to clear unused space.
At the ORM level, the speed issues are because creating objects in Python is slow, and the SQLAlchemy ORM applies a large amount of bookkeeping to these objects as it fetches them, which is necessary in order for it to fulfill its usage contract, including unit of work, identity map, eager loading, collections, etc.
SQLAlchemy delete(...)
method is translated into SQL DELETE FROM
query. This query does not resets any AUTO_INCREMENT counters. To reset a counter in MySQL you have to run a query like that:
ALTER TABLE tbl AUTO_INCREMENT = 100;
If I'm not mistaking, it has no analogue in SQLAlchemy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With