Skip to main content
deleted 29 characters in body; edited tags; edited title
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

How tocan I add entity base field with unique constraint if some entities already exist?

Drupal version 8.

The issue is that adding a base field to a custom entity type in code would result in an issue of unique constraint violation as there does not seem to be a way to preset the value to the newly added field.

setInitialValue()setInitialValue() would set the same value for all the existing entities triggering a constraint violation.

setInitialValueFromField()setInitialValueFromField() would not do any good as it would only allow fetching value from fields of same type. Otherwise it would be possible to utilise the value from an idID field and then replace those initial values with something more suitable.

Is there any meaningful way for doing that?

How to add entity base field with unique constraint if some entities already exist?

Drupal version 8.

The issue is that adding a base field to a custom entity type in code would result in an issue of unique constraint violation as there does not seem to be a way to preset the value to the newly added field.

setInitialValue() would set the same value for all the existing entities triggering a constraint violation.

setInitialValueFromField() would not do any good as it would only allow fetching value from fields of same type. Otherwise it would be possible to utilise the value from an id field and then replace those initial values with something more suitable.

Is there any meaningful way for doing that?

How can I add entity base field with unique constraint if some entities already exist?

The issue is that adding a base field to a custom entity type in code would result in an issue of unique constraint violation as there does not seem to be a way to preset the value to the newly added field.

setInitialValue() would set the same value for all the existing entities triggering a constraint violation.

setInitialValueFromField() would not do any good as it would only allow fetching value from fields of same type. Otherwise it would be possible to utilise the value from an ID field and then replace those initial values with something more suitable.

Is there any meaningful way for doing that?

Source Link
Pjotr
  • 165
  • 9

How to add entity base field with unique constraint if some entities already exist?

Drupal version 8.

The issue is that adding a base field to a custom entity type in code would result in an issue of unique constraint violation as there does not seem to be a way to preset the value to the newly added field.

setInitialValue() would set the same value for all the existing entities triggering a constraint violation.

setInitialValueFromField() would not do any good as it would only allow fetching value from fields of same type. Otherwise it would be possible to utilise the value from an id field and then replace those initial values with something more suitable.

Is there any meaningful way for doing that?