Required field: relation within component

I have a content-type that has a component. this component has a relation field. I want to make the relation field required within the content-type.

I have made a lifecycles.ts for my content-type. It’s working, except for one bug. If I create the component and add a relation, it works. Now, if i delete the component, create it again, and add the same exact relation, it doesn’t. All other use cases are working.

The issue seems specifically to be when I query for the component, the nested relation is returned as null.

Here is my code:

import utils from '@strapi/utils';
const { ApplicationError } = utils.errors;

const serviceFieldError = 'The service component must have a service relation';

module.exports = {
  beforeUpdate(event) {
    return checkAll(event);
  },
};

async function checkAll(event) {
  await checkService(event);
}

/**
 * Check that service is created
 *
 * @param event create/update event
 */
async function checkService(event) {
  const orderId = event?.params?.where?.id;

  console.log(event?.params?.data?.services?.length);

  // if it's an update, make checks
  if (orderId) {
    const order = await strapi.entityService.findOne(
      'api::order.order',
      orderId,
      {
        fields: ['publishedAt'],
      }
    );

    if (!order) {
      throw new ApplicationError(`Order ${orderId} must exist.`);
    }

    // Check if published or is going to be published
    if (event?.params?.data?.publishedAt || order.publishedAt) {
    // Check for services
      if (!event?.params?.data?.services?.length) {
        throw new ApplicationError(serviceFieldError);
      }

      const serviceIds = event?.params?.data?.services?.map((s) => s?.id);

      const services = await strapi.query('order.service').findMany({
        filters: { id: { $in: serviceIds } },
        populate: {
          service: {
            filters: {},
            select: ['id'],
          },
        },
      });

      for (const service of services) {
        // if service is null, throw error
        if (!service.service) {
          throw new ApplicationError(serviceFieldError);
        }
      }
    }
  }
}

console logs when creating the component, and the relation on the first attempt:

1
serviceIds [ 56 ]
services [ { id: 56, items: 1, service: { id: 1 } } ]

on the second attempt:

1
serviceIds [ 57 ]
services [ { id: 57, items: 1, service: null } ]

Correct since the relationship does not yet exist in the database.
So you need to check if it is in the DB or the request you are sending