I get an validation error "relation associated with entity does not exist"

System Information
  • Strapi Version:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

My Controller

makePayment: async (ctx, next) => {
    try {
      const {payee, account,period, packages} = ctx.request.body
      const getAccount = await strapi.db.query("api::client.client").findOne({
        select: ['name'],
        where: { account: account}
      })
      const getPackageAmnt = await strapi.db.query("api::package.package").findOne({
        select: ['amount'],
        where: {name: `${packages}`}
      })
      const data = {
        payee:payee,
        account: getAccount['name'],
        period: period,
        packages: packages,
        TotalAmnt: period * getPackageAmnt['amount']
      }
      const res = await strapi.entityService.create("api::payment.payment", {data:data})
      
      return res
    } catch (err) {
      return err
    }
    
  
  }

My Response

{
  "data": null,
  "error": {
    "status": 400,
    "name": "ValidationError",
    "message": "1 relation(s) of type api::package.package associated with this entity do not exist",
    "details": {
      "errors": [
        {
          "path": [],
          "message": "1 relation(s) of type api::package.package associated with this entity do not exist",
          "name": "ValidationError"
        }
      ]
    }
  }
}

1 Like

So I had this issue and the error was extremely misleading. In my case, the error was thrown in the middle of a database transaction. When I commented out the transaction, I got the real error message and was able to fix it, but it was completely unrelated to the relationship between the two models the validation error gave me.

Hi, @2much!
I encountered the same problem, but it only reproduces in the production environment. Could you please tell me what the problem was in your case?

1 Like

Hi @alstep08, I can’t remember exactly, but I think the error was something straight-forward like calling user.id where user was actually already the ID. The issue was that because it happened during a transaction, the error I received was misleading

Hi @alstep08
Can you please tell me how did you fixed the issue , I am also getting the issue in production database but not in the local and any other new db’s .

It says that the field you are referencing to doesn’t exist. If you are trying to relate a table with custom field instead of the default id that strapi provides, try using this syntax, field: { fieldYouWantToRelate: }, This worked for me