Don't be confused. I am not asking how to drop column in Django. My question is what Django actually does to drop a column from a SQLite database.
Recently i came across an article that says you can't drop columns in SQLite database. So you have to drop the table and recreate it. And its quite strange that if SQLite doesn't support that then how Django is doing that?
Is it doing this?
To drop a column in an SQLite database Django follows the procedure described here: https://www.sqlite.org/lang_altertable.html#caution
Which in simple words is create a new table, copy data from old table, delete old table and then rename new table.
from the source code [GitHub] We can see that the schema editor for SQLite calls self._remake_table(model, delete_field=field)
in the method remove_field
which is what is used to drop a column. The method _remake_table
has the following docstring in it's code which describes how exactly the process is performed:
Shortcut to transform a model from old_model into new_model This follows the correct procedure to perform non-rename or column addition operations based on SQLite's documentation https://www.sqlite.org/lang_altertable.html#caution The essential steps are:
- Create a table with the updated definition called "new__app_model"
- Copy the data from the existing "app_model" table to the new table
- Drop the "app_model" table
- Rename the "new__app_model" table to "app_model"
- Restore any index of the previous "app_model" table.