Django 如何删除 SQLite 中的列
How django drops column in SQLite
不要混淆。我 不是 询问如何在 Django 中删除列。我的问题是 Django 实际上做了什么来从 SQLite 数据库中删除列。
最近我看到一篇文章说你不能删除 SQLite 数据库中的列。所以你必须删除 table 并重新创建它。很奇怪,如果 SQLite 不支持,那么 Django 是怎么做到的?
它在this吗?
要删除 SQLite 数据库中的列,Django 遵循此处描述的过程:https://www.sqlite.org/lang_altertable.html#caution
简单来说就是创建一个新的 table,从旧的 table 复制数据,删除旧的 table,然后重命名新的 table.
来自 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,其中描述了该过程的具体执行方式:
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.
不要混淆。我 不是 询问如何在 Django 中删除列。我的问题是 Django 实际上做了什么来从 SQLite 数据库中删除列。
最近我看到一篇文章说你不能删除 SQLite 数据库中的列。所以你必须删除 table 并重新创建它。很奇怪,如果 SQLite 不支持,那么 Django 是怎么做到的?
它在this吗?
要删除 SQLite 数据库中的列,Django 遵循此处描述的过程:https://www.sqlite.org/lang_altertable.html#caution
简单来说就是创建一个新的 table,从旧的 table 复制数据,删除旧的 table,然后重命名新的 table.
来自 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,其中描述了该过程的具体执行方式:
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.