Mysql 具有多个数据库的web应用程序的单元测试
我有一个django web应用程序,它使用两个数据库:MySQL和PostgreSQL。我想运行一些单元测试,检查对象的创建并读取PostgreSQL数据库中的模型。运行测试数据库时,未创建测试数据库,出现以下错误:Mysql 具有多个数据库的web应用程序的单元测试,mysql,django,database,postgresql,unit-testing,Mysql,Django,Database,Postgresql,Unit Testing,我有一个django web应用程序,它使用两个数据库:MySQL和PostgreSQL。我想运行一些单元测试,检查对象的创建并读取PostgreSQL数据库中的模型。运行测试数据库时,未创建测试数据库,出现以下错误: django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versi
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'jsonb NULL, `processed_payload` jsonb NULL, `scheduled_time` datetime(6) NULL, `' at line 1")
My settings.py文件包含如下数据库条目(将别名而不是实际值放入):
如何确保数据库是为PostgreSQL而不是MySQL创建的,以便创建表查询不会失败
编辑:
包括我创建的用于在运行应用程序时管理数据库选择的APIRouter
class APIrouter(object):
payouts_tables = ['PayoutsConfig', 'PayoutStructures']
def db_for_read(self, model, **hints):
if model.__name__ in self.payouts_tables:
return 'payouts'
else:
return None
def db_for_write(self, model, **hints):
if model.__name__ in self.payouts_tables:
return 'payouts'
return None
def allow_syncdb(self, db, model):
if model.__name__ in self.payouts_tables:
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'payouts':
return db == 'payouts'
return None
还包括我尝试运行的单元测试代码段:
class PayoutsTestCase(TestCase):
def setUp(self):
conf = {"key": "value"}
config = PayoutsConfig.objects.create(config_name="unit_test_config_1",payout_type=1,payout_config=conf)
structure = PayoutStructures.objects.create(structure_name="unit_test_1", payout_configurations=[config])
def test_structure_name(self):
structure = PayoutsConfig.objects.get(structure_name="unit_test_1")
self.assertEqual(structure.name, "unit_test_1")
本试验中使用的模型如下:
class PayoutsConfig(models.Model):
config_name = models.CharField(max_length=100,null=True)
payout_type = models.IntegerField(choices=PayoutTypes.payout_type_choices,null=True)
payout_config = JSONField(null=True)
class PayoutStructures(models.Model):
structure_name = models.CharField(max_length=100,null=True)
payout_configurations = models.ManyToManyField('PayoutsConfig', related_name='structures')
您创建了自定义数据库路由器吗?我创建了一个APIRouter,它也在设置文件中设置。我的应用程序在功能上运行良好。请发布该路由器以及相关模型,包括编辑。
class PayoutsConfig(models.Model):
config_name = models.CharField(max_length=100,null=True)
payout_type = models.IntegerField(choices=PayoutTypes.payout_type_choices,null=True)
payout_config = JSONField(null=True)
class PayoutStructures(models.Model):
structure_name = models.CharField(max_length=100,null=True)
payout_configurations = models.ManyToManyField('PayoutsConfig', related_name='structures')