fix: saving multiple versions (#918)
* test: saving multiple versions with unique fields
* fix: saving multiple versions with unique fields
Version schemas were getting produced with uniqueness constraints which
caused updates to likely fail because each successive version would
potentially reuse unchanged unique key-values (particularly ident keys
like slug) from previous versions.
This was due to `mongoose.buildSchema` not respecting buildSchemaOptions.disableUnique.
This regression was introduced with the fix at commit c175476e.
* test: eslint fix
Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
@@ -80,7 +80,7 @@ const buildSchema = (config: SanitizedConfig, configFields: Field[], buildSchema
|
|||||||
if (config.indexSortableFields && !buildSchemaOptions.global && !field.index && !field.hidden && sortableFieldTypes.indexOf(field.type) > -1 && fieldAffectsData(field)) {
|
if (config.indexSortableFields && !buildSchemaOptions.global && !field.index && !field.hidden && sortableFieldTypes.indexOf(field.type) > -1 && fieldAffectsData(field)) {
|
||||||
indexFields.push({ index: { [field.name]: 1 } });
|
indexFields.push({ index: { [field.name]: 1 } });
|
||||||
} else if (field.unique && fieldAffectsData(field)) {
|
} else if (field.unique && fieldAffectsData(field)) {
|
||||||
indexFields.push({ index: { [field.name]: 1 }, options: { unique: true, sparse: field.localized || false } });
|
indexFields.push({ index: { [field.name]: 1 }, options: { unique: !buildSchemaOptions.disableUnique, sparse: field.localized || false } });
|
||||||
} else if (field.index && fieldAffectsData(field)) {
|
} else if (field.index && fieldAffectsData(field)) {
|
||||||
indexFields.push({ index: { [field.name]: 1 } });
|
indexFields.push({ index: { [field.name]: 1 } });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ describe('Versions', () => {
|
|||||||
|
|
||||||
collectionLocalPostID = autosavePost.id;
|
collectionLocalPostID = autosavePost.id;
|
||||||
|
|
||||||
const updatedPost = await payload.update<any>({
|
const updatedPost = await payload.update({
|
||||||
id: collectionLocalPostID,
|
id: collectionLocalPostID,
|
||||||
collection,
|
collection,
|
||||||
data: {
|
data: {
|
||||||
@@ -82,6 +82,35 @@ describe('Versions', () => {
|
|||||||
expect(collectionLocalVersionID).toBeDefined();
|
expect(collectionLocalVersionID).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow saving multiple versions of models with unique fields', async () => {
|
||||||
|
const autosavePost = await payload.create({
|
||||||
|
collection,
|
||||||
|
data: {
|
||||||
|
title: 'unique unchanging title',
|
||||||
|
description: 'description 1',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await payload.update({
|
||||||
|
id: autosavePost.id,
|
||||||
|
collection,
|
||||||
|
data: {
|
||||||
|
description: 'description 2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const finalDescription = 'final description';
|
||||||
|
|
||||||
|
const secondUpdate = await payload.update({
|
||||||
|
id: autosavePost.id,
|
||||||
|
collection,
|
||||||
|
data: {
|
||||||
|
description: finalDescription,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(secondUpdate.description).toBe(finalDescription);
|
||||||
|
});
|
||||||
|
|
||||||
it('should allow a version to be retrieved by ID', async () => {
|
it('should allow a version to be retrieved by ID', async () => {
|
||||||
const version = await payload.findVersionByID({
|
const version = await payload.findVersionByID({
|
||||||
collection,
|
collection,
|
||||||
|
|||||||
Reference in New Issue
Block a user