问题描述
我想弄清楚为什么我的测试适用于 sqlite3
但不适用于 Postgres
。
导入操作系统
from django.conf import settings
from django.test import TestCase
from products.factory import ProductFactory
from products.models import Product,Organisation
from products.services.imports import ImportService
from users.factory import UserFactory,OrganisationFactory
from users.models import User
class ProductTestCase(TestCase):
@classmethod
def setUpTestData(cls) -> None:
cls.org = OrganisationFactory.create(name='My organisation')
cls.user = User.objects.create_user('peter','xxxxxxxx')
cls.user.organisation = cls.org
cls.product = ProductFactory.create(title='Title',created_by=cls.user,organisation=cls.org)
def test_product_has_organisation(self) -> None:
self.assertTrue(self.product.organisation,'Product has no organisation')
当使用 Postgres
运行它时,它会引发:
ERROR: test_product_has_organisation (products.tests.test_product.ProductTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/milano/PycharmProjects/.virtualenvs/markethelper_api/lib/python3.9/site-packages/django/db/backends/utils.py",line 82,in _execute
return self.cursor.execute(sql)
psycopg2.errors.Foreignkeyviolation: insert or update on table "products_product" violates foreign key constraint "products_product_updated_by_id_33940c75_fk_users_user_id"
DETAIL: Key (updated_by_id)=(19) is not present in table "users_user".
Product.updated_by:
updated_by = CurrentUserField('users.User',on_delete=models.CASCADE,related_name='updated_products',null=True,blank=True,verbose_name='Updated by',on_update=True)
你知道问题出在哪里吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)