fix: do not initialize FKs to null (#10337)

This commit is contained in:
Harminder Virk authored and GitHub committed 2024-11-28 17:15:08 +05:30
1 parent f7279f1b96
commit d57c739052
5 files changed
+52 -22

No files matched your search

@@ -237,7 +237,30 @@ export function defineBelongsToRelationship(
return return
} }
this[foreignKeyName] ??= this[relationship.name]?.id ?? null /**
* Do not override the existing foreign key value if
* exists
*/
if (this[foreignKeyName] !== undefined) {
return
}
/**
* Set the foreign key when the relationship is initialized
* as null
*/
if (this[relationship.name] === null) {
this[foreignKeyName] = null
return
}
/**
* Set the foreign key when the relationship is initialized
* and as the id
*/
if (this[relationship.name] && "id" in this[relationship.name]) {
this[foreignKeyName] = this[relationship.name].id
}
} }
/** /**
@@ -43,7 +43,6 @@ moduleIntegrationTestRunner<Service>({
expect(productCategoryResults).toEqual([ expect(productCategoryResults).toEqual([
expect.objectContaining({ expect.objectContaining({
id: "category-0", id: "category-0",
parent_category_id: null,
}), }),
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -130,7 +129,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -263,9 +261,9 @@ moduleIntegrationTestRunner<Service>({
parent_category_id: "electronics", parent_category_id: "electronics",
parent_category: expect.objectContaining({ parent_category: expect.objectContaining({
id: "electronics", id: "electronics",
parent_category_id: null,
handle: "electronics", handle: "electronics",
mpath: "electronics", mpath: "electronics",
parent_category_id: null,
parent_category: null, parent_category: null,
}), }),
}), }),
@@ -564,7 +562,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -721,6 +718,7 @@ moduleIntegrationTestRunner<Service>({
expect(productCategoryResults[0]).toEqual([ expect(productCategoryResults[0]).toEqual([
expect.objectContaining({ expect.objectContaining({
id: "category-0", id: "category-0",
parent_category_id: null,
parent_category: null, parent_category: null,
}), }),
expect.objectContaining({ expect.objectContaining({
@@ -813,7 +811,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -874,7 +871,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0", id: "category-0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null,
category_children: [ category_children: [
expect.objectContaining({ expect.objectContaining({
id: "category-1", id: "category-1",
@@ -106,8 +106,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
product_id: productOne.id, product_id: productOne.id,
product: { product: {
id: productOne.id, id: productOne.id,
type_id: null,
collection_id: null,
}, },
}, },
]) ])
@@ -183,8 +181,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
product_id: productOne.id, product_id: productOne.id,
product: { product: {
id: productOne.id, id: productOne.id,
type_id: null,
collection_id: null,
}, },
}, },
]) ])
@@ -215,8 +211,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
id: "product-1", id: "product-1",
handle: "product-1", handle: "product-1",
title: "product 1", title: "product 1",
type_id: null,
collection_id: null,
}, },
product_id: "product-1", product_id: "product-1",
}) })
@@ -121,8 +121,33 @@ moduleIntegrationTestRunner<IProductModuleService>({
value: tagOne.value, value: tagOne.value,
products: [ products: [
{ {
id: productOne.id,
},
],
},
])
})
it("should set foreign key to null when relation is select and is null", async () => {
const tags = await service.listProductTags(
{
id: tagOne.id,
},
{
select: ["value", "products.id"],
relations: ["products.collection"],
take: 1,
}
)
expect(tags).toEqual([
{
id: tagOne.id,
value: tagOne.value,
products: [
{
collection: null,
collection_id: null, collection_id: null,
type_id: null,
id: productOne.id, id: productOne.id,
}, },
], ],
@@ -199,8 +224,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
value: tagOne.value, value: tagOne.value,
products: [ products: [
{ {
collection_id: null,
type_id: null,
id: productOne.id, id: productOne.id,
}, },
], ],
@@ -492,21 +492,18 @@ moduleIntegrationTestRunner<Service>({
name: "category 0", name: "category 0",
handle: "category-0", handle: "category-0",
mpath: "category-0", mpath: "category-0",
parent_category_id: null,
}, },
{ {
id: "category-1", id: "category-1",
name: "category 1", name: "category 1",
handle: "category-1", handle: "category-1",
mpath: "category-0.category-1", mpath: "category-0.category-1",
parent_category_id: null,
}, },
{ {
id: "category-1-a", id: "category-1-a",
name: "category 1 a", name: "category 1 a",
handle: "category-1-a", handle: "category-1-a",
mpath: "category-0.category-1.category-1-a", mpath: "category-0.category-1.category-1-a",
parent_category_id: null,
}, },
]) ])
}) })
@@ -605,7 +602,6 @@ moduleIntegrationTestRunner<Service>({
title: workingProduct.title, title: workingProduct.title,
handle: "product-1", handle: "product-1",
collection_id: workingCollection.id, collection_id: workingCollection.id,
type_id: null,
collection: { collection: {
handle: "col-1", handle: "col-1",
id: workingCollection.id, id: workingCollection.id,
@@ -639,7 +635,6 @@ moduleIntegrationTestRunner<Service>({
id: workingProduct.id, id: workingProduct.id,
title: workingProduct.title, title: workingProduct.title,
handle: "product-1", handle: "product-1",
type_id: null,
collection_id: workingCollection.id, collection_id: workingCollection.id,
collection: { collection: {
handle: "col-1", handle: "col-1",
@@ -651,7 +646,6 @@ moduleIntegrationTestRunner<Service>({
id: workingProductTwo.id, id: workingProductTwo.id,
title: workingProductTwo.title, title: workingProductTwo.title,
handle: "product", handle: "product",
type_id: null,
collection_id: workingCollectionTwo.id, collection_id: workingCollectionTwo.id,
collection: { collection: {
handle: "col-2", handle: "col-2",