mirror of
https://github.com/directus/directus.git
synced 2026-01-29 23:47:57 -05:00
Change relation onDelete rule to NO ACTION when the field is set non-nullable (#11597)
This commit is contained in:
@@ -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,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user