Open skid opened 2 years ago
The solution here would be to remove the unique constraint at the DB level and do a check for uniqueness among non-deleted rows, like we already do with Customer entitities.
Since v1.6 we can remove the unique constraint at the database level using the following metadata modifier:
import { Administrator, EntityMetadataModifier } from '@vendure/core';
import { Column } from 'typeorm';
export const removeAdministratorEmailAddressUniqueConstraint: EntityMetadataModifier = metadata => {
const administratorEmailIndex = metadata.columns.findIndex(
col => col.propertyName === 'emailAddress' && col.target === Administrator,
);
const administratorUniqueCols = metadata.uniques.find(arg => arg.target === Administrator)?.columns;
if (-1 < administratorEmailIndex && Array.isArray(administratorUniqueCols)) {
const emailAddressIndex = administratorUniqueCols.findIndex(name => name === 'emailAddress');
if (emailAddressIndex > -1) {
if (administratorUniqueCols.length === 1) {
const administratorUniqueIndex = metadata.uniques.findIndex(
arg => arg.target === Administrator,
);
if (administratorUniqueIndex > -1) {
metadata.uniques.splice(administratorUniqueIndex, 1);
}
} else {
administratorUniqueCols.splice(emailAddressIndex, 1);
}
}
metadata.columns[administratorEmailIndex].options.unique = false;
}
};
// vendure-config.ts
export const devConfig: VendureConfig = {
entityOptions: {
metadataModifiers: [removeAdministratorEmailAddressUniqueConstraint],
},
// ...
};
Not sure if this question belongs here, but what if we want to do a "hard delete" on different Entities?
Using the above solution is only for creating an Administrator with duplicate-email that the soft-deleted one has.
Currently I have to always delete the specific row from Administrator-table with SQL or a DB-client to be able to create Administrator with same email again.
Currently there is not built-in "hard delete" for soft-delete entities, so yes you need to manually delete rows from the DB.
You can also make this easier by creating a plugin which exposes a hard-delete mutation and then expose this in the Admin UI via extension.
Describe the bug If you delete an administrator and try to create another with the same email, you get a database unique constraint error.
Expected behavior Soft delete working properly (or no soft delete at all)
Environment (please complete the following information):