How to ensure an entity has relationship to another entity

I have…

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [ ] Bug report
  • [ ] Performance issue
  • [x] Documentation issue or request

Current behavior

My issue is that i have two entities, a calendar which has multiple events. I have set a rule that requires an event to have exactly one calendar. The problem is if a parent calendar is deleted this removes the calendar from the events and we are left with orphaned events.

I realise this means we cannot guarantee that an event has an associated calendar however I also cannot filter these orphaned events out using an odata query.

{
  "filter": "data/calendar/iv/$count eq 0"
}

To which i get the response " “message”: “OData $filter clause not valid: The next token in a CountSegmentNode must be a collection.”,"

I assume this is because Squidex doesn’t support all odata.

I cannot deal with this server side as filtering out some results will cause problems with pagination.

Any advice on how to handle this? The only thing I can think of is to use a hook to go in and delete the orphaned events after the fact

There is an empty operator that should work:

{
  "filter": "empty(data/calendar/iv)"
}

Are these operators documented anywhere? I am looking for events with a calendar. My test query should have looked like

{
  "filter": "data/calendar/iv/$count gt 0"
}

Squidex only supports a subset of OData. It is documented here: https://docs.squidex.io/02-documentation/developer-guides/api-overview/api

But empty is missing, I have to add it.

not(empty(data/calendar/iv))

worked for me fwiw

1 Like

Unfortunately gitbook does not work at the moment, otherwise I would have added the empty sample to the documentation.

Hey @Sebastian, not(empty(data/calendar/iv)) suddenly stopped working. empty(data/calendar/iv) also filters all results. Have there been any changes?

You are talking about the cloud, right? There have been no changes. If you give me your app and schema I can have a look.

Yes, thank you
asap-hub-dev
Events schema

query FetchEvents($top: Int, $skip: Int, $filter: String, $order: String) {
  queryEventsContentsWithTotal(
    top: $top
    skip: $skip
    filter: $filter
    orderby: $order
  ) {
    total
    items {
      flatData {
        calendar {
          flatData {
            googleCalendarId
            color
            name
          }
        }
      }
    }
  }
}

{
  "filter": "not(empty(data/calendar/iv))"
}

Okay, I understand the problem now.

The root cause is how references are handled. When you have a reference the ID is added to the document in the database.

Before the content is returned by the API, the API checks whether the references are still valid and removes all non-existing references from the API result, but not from the database. It cannot modify the database because this could cause issues when the content is modified at the same time.

Your content in the database still has a reference to a calendar entry:

"calendar" : {
            "iv" : [
                "7c206965-f39f-4f44-a392-c6c7001eab63"
            ]
        },

Therefore the query operator does not work, because when the query is executed it is converted to a database filter and for the database the calendar field is not empty.

Right now I do not have a solution for that, I am sorry.