engine icon indicating copy to clipboard operation
engine copied to clipboard

Always forbid NULL values in array elements

Open matej21 opened this issue 10 months ago • 1 comments

Currently, when defining an array column in the Contember schema using the .list() method, the .notNull() constraint applies only to the column itself. This means that while the column cannot be NULL, the array may still contain NULL elements. For example:

someValues = c.stringColumn().list().notNull()

allows:

  • An empty array: []
  • Arrays with NULL elements, e.g., [NULL], [value, NULL]

Proposal

We propose to change the behavior for array columns so that NULL values in the array elements are always forbidden. This change is independent of the .notNull() constraint on the column, which will continue to only ensure that the column itself is non-NULL.

Under this proposal:

  • Column Constraint (.notNull()):
    Prevents the entire column from being NULL.

  • Element Constraint (Always Active for Arrays):
    Enforces that no NULL values can exist within the array. This could be implemented with a check constraint similar to:

    CHECK (array_position(foo, null) IS NULL)
    

Request for Feedback

  • Do you agree with the approach of always forbidding NULL values in array elements?
  • Can you identify any use cases or scenarios where having NULL elements in an array would be beneficial?

matej21 avatar Feb 26 '25 16:02 matej21