fix: do not initialize FKs to null (#10337)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -43,7 +43,6 @@ moduleIntegrationTestRunner<Service>({
|
||||
expect(productCategoryResults).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "category-0",
|
||||
parent_category_id: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: "category-1",
|
||||
@@ -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",
|
||||
@@ -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,
|
||||
}),
|
||||
}),
|
||||
@@ -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",
|
||||
@@ -721,6 +718,7 @@ moduleIntegrationTestRunner<Service>({
|
||||
expect(productCategoryResults[0]).toEqual([
|
||||
expect.objectContaining({
|
||||
id: "category-0",
|
||||
parent_category_id: null,
|
||||
parent_category: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
|
||||
-6
@@ -106,8 +106,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
type_id: null,
|
||||
collection_id: null,
|
||||
},
|
||||
},
|
||||
])
|
||||
@@ -183,8 +181,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
product_id: productOne.id,
|
||||
product: {
|
||||
id: productOne.id,
|
||||
type_id: null,
|
||||
collection_id: null,
|
||||
},
|
||||
},
|
||||
])
|
||||
@@ -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",
|
||||
})
|
||||
|
||||
+26
-3
@@ -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,
|
||||
},
|
||||
],
|
||||
@@ -199,8 +224,6 @@ moduleIntegrationTestRunner<IProductModuleService>({
|
||||
value: tagOne.value,
|
||||
products: [
|
||||
{
|
||||
collection_id: null,
|
||||
type_id: null,
|
||||
id: productOne.id,
|
||||
},
|
||||
],
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
])
|
||||
})
|
||||
@@ -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,
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user