#NestJS / TypeORM - NULL Value In `uniqueCookingEffectId` Field

3 messages · Page 1 of 1 (latest)

dire flame
#

The Repo: https://github.com/rscottlundgren/hyrule-gis/tree/66-add-relations-between-material-unique_cooking_effect-entity

The Error:

  • When I perform a POST request to the 'material' endpoint, I get confirmation that a relationship has been established, but when I check the database the 'uniqueCookingEffectId' column has a NULL value in the 'material' table. (EDIT: The first bullet has been solved).
  • There is no corresponding column in 'unique_cooking_effect' that shows there are potentially many 'material' records that share that same 'unique_cooking_effect'. (EDIT: The second bullet has been solved).

Expected Behavior: When a User makes a POST request to the 'material' endpoint, the API will check to see if there's a match to a record in the 'unique_cooking_effect' table based on name. If such a value exists, a one-to-many / many-to-one connection is made between those two record rows and those key / foreign key relations are established in the DB.

Reproduction Steps: Reproduction steps have been provided in a subsequent comment as I will hit the character limit otherwise.

Additional Notes: I have a feeling that the Root Cause of this is that I'm not creating an appropriate relationship between the tables for TypeORM or NestJS to know that a relationship should exist and be recorded. Unfortunately, I'm not able to test this as when I try to move the 'UniqueCookingEffectsService' into the 'TypeORM.forFeature()' method NestJS throws an error.

#

Reproduction Steps:

  1. Start up the server npm run start:dev
  2. Make the following request:
curl --location 'http://localhost:3000/unique-cooking-effects' \
--header 'Content-Type: application/json' \
--data '{
    "name": "Effect Duration +00:20",
    "description": "Increases the duration of the effect by a specified number of seconds / minutes."
}'
  1. Make the following request:
curl --location 'http://localhost:3000/materials' \
--header 'Content-Type: application/json' \
--data '{
    "id": 204,
    "name": "Golden Apple",
    "description": "A rare, very sweet fruit that seems to have an extra sparkle to it. Sometimes found near normal apples.",
    "fuse_attack_power": 1.0,
    "hearts_recovered": 1.5,
    "unique_cooking_effect": "Effect Duration +00:20",
    "common_locations": ["Hyrule Field", "Hyrule Ridge"],
    "tradeable": true
}'
  1. Check the 'material' table in the DB to see that there is a NULL value for 'uniqueCookingEffectId'. Alternatively, you can make the following request:
curl --location 'http://localhost:3000/materials'
dire flame
#

Ok - this one was solved through "trial and error"

The first solution was the result of the fact that I - originally - was not accommodating for a returned array value. I was expecting an object but after going through the code, line by line, I was able to determine that the problem was the fact that I wasn't pulling out the specific index of the array and instead was assigning the whole array to the field, resulting in a NULL value

The second solution was that I needed to reset my mental understanding of how relations are displayed in the actual DB versus how they will be displayed when called. As previously stated, I was expecting for there to be a field in 'unique_cooking_effect' that showed an array of 'material' objects. That field does exist, but it doesn't show in the database. Running a GET request on 'unique_cooking_effect' shows this.

Thanks for letting me, once again, "rubber duck" in the channel.