vendure-ecommerce/vendure

Readonly, Nullable and Unique when adding CustomFields

Closed this issue · 2 comments

Describe the bug
This bug refers to some issues related to the configurations for customFields:

  1. When defining a customField as readOnly, if I update a product in the admin UI, the product looses the value for the readOnly fields.
  2. If a customField is configured as unique and nullable, we get an error if the field has the value NULL, as there is a Key constraint which is not being fulfilled if there are more than one product that have that field as NULL.

To Reproduce
Steps to reproduce the behavior:

  1. Create a custom field as readOnly.

  2. Create a product using the ProductService, and define the value for that field.

  3. Update the product through the adminUI.

  4. Verify that the readOnly field lost its value.

  5. Create a custom field as unique and nullable.

  6. Create a product and leave the new field empty.

  7. Create a second product and leave the field empty as well.

  8. An error will appear saying the key constraint is not met.

Expected behavior

  1. The readOnly field should not dissapear when the product is updated.
  2. When configuring a customField as nullable and unique, the constraint shouldn't apply to the NULL value.

Environment (please complete the following information):

  • @vendure/core version: 2.2.2
  • Nodejs version: 22.1.0
  • Database (mysql/postgres etc): Postgres

Regarding readonly field data being lost, I cannot reproduce this on current master:

Untitled.video.1.mp4

Please check on the latest version of Vendure, and if you can still reproduce, please provide the exact custom field config you used.


Regarding nullable unique fields, it looks like in Postgres at least there is a possibility to support this case with the UNIQUE NULLS NOT DISTINCT directive (https://blog.rustprooflabs.com/2022/07/postgres-15-unique-improvement-with-null)

There's an open issue for this on the TypeORM repo: typeorm/typeorm#9827

I updated the Postgres version to 15, which solved the unique and nullable issue. I also updated my project's vendure version to 2.2.6 where I can't reproduce the issue with the readOnly fields anymore.