Django - 擦除django生成的数据库索引是否可以?

时间:2021-09-12 23:47:35

AFAIK django creates indexes for you in the database (I use MySQL). As these indexes are preventing me from altering column names as part of a south migration I'm performing, I would like to remove these indexes.

AFAIK django在数据库中为您创建索引(我使用MySQL)。由于这些索引阻止我改变列名称作为我正在执行的南迁移的一部分,我想删除这些索引。

Is it legitimate to manually erase django indexes?
Will django recreate them?

手动擦除django索引是否合法? django会重新创造它们吗?

1 个解决方案

#1


1  

I guess you are altering a unique_together constraint that is stored as an index, in mysql.

我想你正在改变在mysql中存储为索引的unique_together约束。

And, no django doesn't create it if you delete it, on the fly.

而且,如果你在飞行中删除它,任何django都不会创建它。

If you delete it manually, you need to create the relevant one, manually as well.

如果手动删除它,则还需要手动创建相关的一个。

You can see the one that should be created, by ./manage.py sqlall appname

您可以通过./manage.py sqlall appname查看应该创建的那个

Update (based on your comments):

Jonathan, you are saying that you are trying to migrate.

乔纳森,你说你正在尝试迁移。

So, I guess you have already changed the model, and the index that is existing in the db is the one generated in an earlier state of models, at which your syncdb was executed;

所以,我猜你已经改变了模型,并且db中存在的索引是在早期模型状态下生成的索引,在该状态下执行syncdb;

Also note that mysql stores unique_together constraints as indexes, and so it adds relevant indexes even without you (or django in this case) having to explicitly ask for it.

另请注意,mysql将unique_together约束存储为索引,因此即使没有您(或在这种情况下为django)必须明确要求它,它也会添加相关索引。

#1


1  

I guess you are altering a unique_together constraint that is stored as an index, in mysql.

我想你正在改变在mysql中存储为索引的unique_together约束。

And, no django doesn't create it if you delete it, on the fly.

而且,如果你在飞行中删除它,任何django都不会创建它。

If you delete it manually, you need to create the relevant one, manually as well.

如果手动删除它,则还需要手动创建相关的一个。

You can see the one that should be created, by ./manage.py sqlall appname

您可以通过./manage.py sqlall appname查看应该创建的那个

Update (based on your comments):

Jonathan, you are saying that you are trying to migrate.

乔纳森,你说你正在尝试迁移。

So, I guess you have already changed the model, and the index that is existing in the db is the one generated in an earlier state of models, at which your syncdb was executed;

所以,我猜你已经改变了模型,并且db中存在的索引是在早期模型状态下生成的索引,在该状态下执行syncdb;

Also note that mysql stores unique_together constraints as indexes, and so it adds relevant indexes even without you (or django in this case) having to explicitly ask for it.

另请注意,mysql将unique_together约束存储为索引,因此即使没有您(或在这种情况下为django)必须明确要求它,它也会添加相关索引。