报错:
ValueError: Cannot assign "<...>": the current database router prevents this relation.
中文:
数值异常:无法分配…:当前的数据库路由器阻止了这种关系。
解析:
1.项目设置了router读写分离,且在ORM create()方法中,使用了前边filter()方法得到的数据,
2.由于django是惰性查询,前边的filter()并没有立即查询,而是在create()中引用了filter()的数据时,执行了filter(),
3.此时写操作的db指针指向write_db,filter()的db指针指向read_db,两者发生冲突,导致服务禁止了此次与mysql的交互
解决办法:
在前边filter()方法中,使用using()方法,使filter()方法立即与数据库交互,查出数据。
如:
ret = Models.objects.using('default').filter(id=1)
NewModels.objects.create(name=ret.first().name)