Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django unittest给出`无法添加或更新子行:外键约束失败`_Django_Unit Testing - Fatal编程技术网

Django unittest给出`无法添加或更新子行:外键约束失败`

Django unittest给出`无法添加或更新子行:外键约束失败`,django,unit-testing,Django,Unit Testing,我有3个不同的应用,如果我运行的话,每个应用都有unittest python manage.py test categories coupons stores 我会得到 IntegrityError(1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_gocoupons`.`coupons_coupon`, CONSTRAINT `store_id_refs_id_bf6c0d9e` F

我有3个不同的应用,如果我运行的话,每个应用都有unittest

python manage.py test categories coupons stores
我会得到

IntegrityError(1452, 'Cannot add or update a child row: a foreign key constraint fails (`test_gocoupons`.`coupons_coupon`, CONSTRAINT `store_id_refs_id_bf6c0d9e` FOREIGN KEY (`store_id`) REFERENCES `stores_store` (`id`))'))
但是,如果我单独运行它们,它们就可以了

这是一个所有测试用例都使用的测试混合:

# G is a function to create fixtures see django-dynamic-fixtures

class GoCouponsTestCaseMixin(object):
    categories = []
    stores = []
    coupons = []
    offers = []

    def setUp(self):
        self.categories.append(G(Category))
        self.stores.append(G(Store, is_active=True))
        # make sure we generate enough coupons for page 2
        for i in range(1, settings.COUPON_PAGE_SIZE + 10):
            coupon = G(Coupon, title='free', store=self.stores[0], is_active=True)
            CouponCategory.objects.create(coupon=coupon, category=self.categories[0])
            Offer.objects.create(start=timezone.now() + timedelta(days=-10), expire=timezone.now() + timedelta(days=10), coupon=coupon)
            self.coupons.append(coupon)
Stacktrace显示该行导致问题:

coupon = G(Coupon, title='free', store=self.stores[0], is_active=True)

看起来这是由django dynamic fixtures引起的,添加了几个查询来获取对象,看起来是为了提交插入查询

class GoCouponsTestCaseMixin(object):
    categories = []
    stores = []
    coupons = []
    offers = []

    def setUp(self):
        self.categories.append(G(Category))
        self.stores.append(G(Store, is_active=True))

        # manually querying the objects fixed it
        store = Store.objects.first()
        category = Category.objects.first()

        # make sure we generate enough coupons for page 2
        for i in range(1, settings.COUPON_PAGE_SIZE + 10):
            coupon = G(Coupon, title='free', store=store, is_active=True)
            CouponCategory.objects.create(coupon=coupon, category=category)
            Offer.objects.create(start=timezone.now() + timedelta(days=-10), expire=timezone.now() + timedelta(days=10), coupon=coupon)
            self.coupons.append(coupon)

哪个数据库?如果是MySQL,哪个存储引擎?您不需要删除
tearDown
中的所有内容。Django为每个测试重新创建数据库。@sk1p我正在使用MySQL,我看了其他问题,他们说这是由于不同的引擎,但我的网站运行良好。@DanielRoseman哦,好的,我以前没有tearDown功能,我认为记录没有被删除,所以我添加了它。