Skip to content

Commit

Permalink
fix: do not initialize FKs to null (#10337)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Nov 28, 2024
1 parent f7279f1 commit d57c739
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,30 @@ export function defineBelongsToRelationship(
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
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ moduleIntegrationTestRunner<Service>({
expect(productCategoryResults).toEqual([
expect.objectContaining({
id: "category-0",
parent_category_id: null,
}),
expect.objectContaining({
id: "category-1",
Expand Down Expand Up @@ -130,7 +129,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
category_children: [
expect.objectContaining({
id: "category-1",
Expand Down Expand Up @@ -263,9 +261,9 @@ moduleIntegrationTestRunner<Service>({
parent_category_id: "electronics",
parent_category: expect.objectContaining({
id: "electronics",
parent_category_id: null,
handle: "electronics",
mpath: "electronics",
parent_category_id: null,
parent_category: null,
}),
}),
Expand Down Expand Up @@ -564,7 +562,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
category_children: [
expect.objectContaining({
id: "category-1",
Expand Down Expand Up @@ -721,6 +718,7 @@ moduleIntegrationTestRunner<Service>({
expect(productCategoryResults[0]).toEqual([
expect.objectContaining({
id: "category-0",
parent_category_id: null,
parent_category: null,
}),
expect.objectContaining({
Expand Down Expand Up @@ -813,7 +811,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
category_children: [
expect.objectContaining({
id: "category-1",
Expand Down Expand Up @@ -874,7 +871,6 @@ moduleIntegrationTestRunner<Service>({
id: "category-0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
category_children: [
expect.objectContaining({
id: "category-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
product_id: productOne.id,
product: {
id: productOne.id,
type_id: null,
collection_id: null,
},
},
])
Expand Down Expand Up @@ -183,8 +181,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
product_id: productOne.id,
product: {
id: productOne.id,
type_id: null,
collection_id: null,
},
},
])
Expand Down Expand Up @@ -215,8 +211,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
id: "product-1",
handle: "product-1",
title: "product 1",
type_id: null,
collection_id: null,
},
product_id: "product-1",
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,33 @@ moduleIntegrationTestRunner<IProductModuleService>({
value: tagOne.value,
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,
type_id: null,
id: productOne.id,
},
],
Expand Down Expand Up @@ -199,8 +224,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
value: tagOne.value,
products: [
{
collection_id: null,
type_id: null,
id: productOne.id,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,21 +492,18 @@ moduleIntegrationTestRunner<Service>({
name: "category 0",
handle: "category-0",
mpath: "category-0",
parent_category_id: null,
},
{
id: "category-1",
name: "category 1",
handle: "category-1",
mpath: "category-0.category-1",
parent_category_id: null,
},
{
id: "category-1-a",
name: "category 1 a",
handle: "category-1-a",
mpath: "category-0.category-1.category-1-a",
parent_category_id: null,
},
])
})
Expand Down Expand Up @@ -605,7 +602,6 @@ moduleIntegrationTestRunner<Service>({
title: workingProduct.title,
handle: "product-1",
collection_id: workingCollection.id,
type_id: null,
collection: {
handle: "col-1",
id: workingCollection.id,
Expand Down Expand Up @@ -639,7 +635,6 @@ moduleIntegrationTestRunner<Service>({
id: workingProduct.id,
title: workingProduct.title,
handle: "product-1",
type_id: null,
collection_id: workingCollection.id,
collection: {
handle: "col-1",
Expand All @@ -651,7 +646,6 @@ moduleIntegrationTestRunner<Service>({
id: workingProductTwo.id,
title: workingProductTwo.title,
handle: "product",
type_id: null,
collection_id: workingCollectionTwo.id,
collection: {
handle: "col-2",
Expand Down

0 comments on commit d57c739

Please sign in to comment.