-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
API: Set next value on sequence for AUTO_INCREMENT
primary keys
#19194
Conversation
🦋 Changeset detectedLatest commit: 0078ec7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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') { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
directus/packages/schema/src/dialects/postgres.ts
Lines 185 to 187 in 5107c13
if (column.is_identity || column.default_value?.startsWith('nextval(')) { | |
column.default_value = 'AUTO_INCREMENT'; | |
} else { |
directus/packages/schema/src/dialects/sqlite.ts
Lines 62 to 65 in 5107c13
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), |
directus/packages/schema/src/dialects/cockroachdb.ts
Lines 190 to 192 in 5107c13
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), |
Closing in favor of #19328. |
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