veryacademy/django-ecommerce-project-v2

Problem installing fixtures: insert or update on table "inventory_productinventory" violates foreign key constraint

shakori999 opened this issue · 0 comments

hello, sir.
I tried to test my db
after ending part 9 of this tut

with this test

@pytest.mark.dbfixture
@pytest.mark.parametrize(
    "id, name, slug, is_active",
    [
        (1, "fashion", "fashion", 1),
        # (2, "woman", "woman", 1),
        # (3, "shoes", "shoes", 1),
    ],
)
def test_inventory_category_dbfixture(db, db_fixture_setup, id, name, slug, is_active):
    result = Category.objects.get(id=id)
    assert result.name == name
    assert result.slug == slug
    assert result.is_active == is_active

and this is my db_fixtur_setupe.py


@pytest.fixture(scope="session")
def db_fixture_setup(django_db_setup, django_db_blocker):
    """
    Load DB data fixtures
    """
    with django_db_blocker.unblock():
        call_command("loaddata", "db_product_inventory_fixture.json")
        call_command("loaddata", "db_type_fixture.json")
        call_command("loaddata", "db_product_attribute_fixture.json")
        call_command("loaddata", "db_product_fixture.json")
        call_command("loaddata", "db_category_fixture.json")
        call_command("loaddata", "db_brand_fixture.json")
        call_command("loaddata", "db_product_attribute_value_fixture.json")
        call_command("loaddata", "db_media_fixture.json")
        call_command("loaddata", "db_stock_fixture.json")
        call_command("loaddata", "db_product_attribute_values_fixture.json")
        # call_command("loaddata", "db_product_type_attribute_fixture.json")

this is the first Item in db_product_inventory.json

[
    {
        "model": "inventory.productinventory",
        "id": "1",
        "fields": {
            "sku": "7633969397",
            "upc": "100000000001",
            "product_type": "1",
            "product": "1",
            "brand": "1",
            "is_active": "1",
            "is_default": "1",
            "retail_price": "97",
            "store_price": "92",
            "is_digital": "False",
            "weight": "987",
            "created_at": "2021-09-01T13:20:30+03:00",
            "updated_at": "2021-09-01T13:20:30+03:00"
        }
    },
]

and when I ran the test i got this error

django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table "inventory_productinventory" violates foreign key constraint "inventory_productinv_product_type_id_fa69a5b2_fk_inventory"      
E               DETAIL:  Key (product_type_id)=(1) is not present in table "inventory_producttype".

i have a product_type fixture in db_type_fixture.json

[
    {
        "model": "inventory.producttype",
        "id": "1",
        "fields": {
            "name": "shoes"
        }
    }
]

so what is the problem here? pls if you know any info about this,
i googled it and they say: it tried to add the item before its table are build , so I guess in my case
i tried to find the product_type_id=1 in product_inventorytable before I have the product_typetable in my database !

I tried to load db_type_fixture.json befor db_product_inventory_fixture.json and still the same error,
I tried to deleted this line call_command("loaddata", "db_type_fixture.json") and got the same error
so I guess this line doesn't work or there something else with this line
call_command("loaddata", "db_product_inventory_fixture.json")