我为Django项目设置了两个数据库,一个MySQL和一个Postgres。
我已经安装了一个数据库路由器,并且一切正常,直到在Postgres模型中将CharField
和max_length
的值更改为300为止。
该模型在Postgres中,但是现在Django抛出有关MySQL的错误:
[postgres_app.TestModel.slug]: (mysql.E001) MySQL does not allow unique CharFields to have a max_length > 255.
我仔细检查了数据库路由器,一切看起来都很好。我还必须再次提到路由器之前已正确完成其工作,并选择了正确的数据库进行读取,写入和迁移。这是我的路由器:
POSTGRES_APPS = ['postgres_app']
class PostgresRouter(object):
""" Controls which apps can have access to the postgres instance. """
def db_for_read(self, model, **hints):
""" Called on every read action on a model. """
if model._meta.app_label in POSTGRES_APPS:
return 'postgres'
return None
def db_for_write(self, model, **hints):
""" Called on every write action on a model. """
if model._meta.app_label in POSTGRES_APPS:
return 'postgres'
return None
def allow_relation(self, obj1, obj2, **hints):
""" Called only on migrations. Only allow relations if both models are in POSTGRES_APPS. """
return {obj1._meta.app_label, obj2._meta.app_label}.issubset(set(POSTGRES_APPS)) or None
def allow_migrate(self, db, app_label, model_name=None, **hints):
""" Called only on migrations. """
if db == 'postgres':
return app_label in POSTGRES_APPS
else:
return app_label not in POSTGRES_APPS
这是我的模型:
class TestModel(models.Model):
slug = models.CharField(max_length=300, blank=True, null=True, unique=True)
所以问题是,这是否是Django中的错误,如果不是,那么如何告诉Django它正在查看错误的数据库。