Change relation onDelete rule to NO ACTION when the field is set non-nullable (#11597)

This commit is contained in:
Oreille
2022-02-16 16:00:56 +01:00
committed by GitHub
parent de5b34d87b
commit 7c35f3b7aa
3 changed files with 40 additions and 25 deletions

View File

@@ -51,29 +51,7 @@
v-model="onDeleteRelated"
:disabled="collection === relatedCollection"
:placeholder="t('choose_action') + '...'"
:items="[
{
text: t('referential_action_set_null', { field: currentField }),
value: 'SET NULL',
},
{
text: t('referential_action_set_default', { field: currentField }),
value: 'SET DEFAULT',
},
{
text: t('referential_action_cascade', {
collection: collection,
field: currentField,
}),
value: 'CASCADE',
},
{
text: t('referential_action_no_action', {
field: currentField,
}),
value: 'NO ACTION',
},
]"
:items="onDeleteOptions"
/>
</div>
</div>
@@ -152,6 +130,30 @@ export default defineComponent({
return t('add_field_related');
});
const onDeleteOptions = computed(() =>
[
{
text: t('referential_action_set_null', { field: currentField.value }),
value: 'SET NULL',
},
{
text: t('referential_action_set_default', { field: currentField.value }),
value: 'SET DEFAULT',
},
{
text: t('referential_action_cascade', {
collection: collection.value,
field: currentField.value,
}),
value: 'CASCADE',
},
{
text: t('referential_action_no_action', { field: currentField.value }),
value: 'NO ACTION',
},
].filter((o) => !(o.value === 'SET NULL' && field.value.schema?.is_nullable === false))
);
return {
t,
collection,
@@ -164,6 +166,7 @@ export default defineComponent({
correspondingFieldKey,
generationInfo,
onDeleteRelated,
onDeleteOptions,
};
},
});

View File

@@ -2,7 +2,7 @@ import { StateUpdates, State, HelperFunctions } from '../types';
import { set } from 'lodash';
export function applyChanges(updates: StateUpdates, state: State, helperFn: HelperFunctions) {
const { hasChanged } = helperFn;
const { hasChanged, getCurrent } = helperFn;
if (hasChanged('localType')) {
setTypeToUUID(updates);
@@ -12,6 +12,12 @@ export function applyChanges(updates: StateUpdates, state: State, helperFn: Help
if (hasChanged('field.field')) {
updateRelationField(updates);
}
if (hasChanged('field.schema.is_nullable')) {
if (updates.field?.schema?.is_nullable === false && getCurrent('relations.m2o.schema.on_delete') === 'SET NULL') {
set(updates, 'relations.m2o.schema.on_delete', 'NO ACTION');
}
}
}
export function setTypeToUUID(updates: StateUpdates) {

View File

@@ -3,7 +3,7 @@ import { set } from 'lodash';
import { useCollectionsStore, useFieldsStore } from '@/stores';
export function applyChanges(updates: StateUpdates, state: State, helperFn: HelperFunctions) {
const { hasChanged } = helperFn;
const { hasChanged, getCurrent } = helperFn;
if (hasChanged('localType')) {
prepareRelation(updates, state);
@@ -22,6 +22,12 @@ export function applyChanges(updates: StateUpdates, state: State, helperFn: Help
if (hasChanged('fields.corresponding')) {
setRelatedOneFieldForCorrespondingField(updates);
}
if (hasChanged('field.schema.is_nullable')) {
if (updates.field?.schema?.is_nullable === false && getCurrent('relations.m2o.schema.on_delete') === 'SET NULL') {
set(updates, 'relations.m2o.schema.on_delete', 'NO ACTION');
}
}
}
export function prepareRelation(updates: StateUpdates, state: State) {