Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Set next value on sequence for AUTO_INCREMENT primary keys #19194

Conversation

joselcvarela
Copy link
Member

Motivation

Sometimes I have seen the error ID: Value has to be unique while creating new items.
This seems to happen specially after importing items to a collection using the utility tool.

Solution

In order to prevent this, we can just check if the primary field of the collection is AUTO_INCREMENT, then fetch the maximum value on primary key column and increment 1 to that value.

Fixes #6321
Fixes #18662

@changeset-bot
Copy link

changeset-bot bot commented Jul 18, 2023

🦋 Changeset detected

Latest commit: 0078ec7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@directus/api Patch
directus Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR


try {
if (!payloadWithoutAliases[primaryKeyFieldName] && primaryKeyField?.defaultValue === 'AUTO_INCREMENT') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AUTO_INCREMENT seems to be constant across DB dialects

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the @directus/schema sets the default value as AUTO_INCREMENT accordingly. 👍

Reference

if (column.is_identity || column.default_value?.startsWith('nextval(')) {
column.default_value = 'AUTO_INCREMENT';
} else {

default_value:
column.pk === 1 && tablesWithAutoIncrementPrimaryKeys.includes(table)
? 'AUTO_INCREMENT'
: parseDefaultValue(column.dflt_value),

default_value: column.extra === 'auto_increment' ? 'AUTO_INCREMENT' : parseDefaultValue(column.default_value),

if (column.is_identity || column.default_value?.startsWith('nextval(')) {
column.default_value = 'AUTO_INCREMENT';
} else {

default_value: column.is_identity ? 'AUTO_INCREMENT' : parseDefaultValue(column.default_value),

default_value: hasAutoIncrement ? 'AUTO_INCREMENT' : parseDefaultValue(column.default_value),

@jaads jaads self-requested a review July 26, 2023 08:04
@paescuj
Copy link
Member

paescuj commented Aug 2, 2023

Closing in favor of #19328.

@paescuj paescuj closed this Aug 2, 2023
@joselcvarela joselcvarela deleted the api/fix-insert-sequence-conflict branch August 2, 2023 12:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
3 participants