-
-
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
Composite "Unique" constraint #15063
Comments
This was actually added! However it seems like it's not working for MySQL since As it's never returned, it's causing the
Error response for MySQL (missing field): {
"errors": [
{
"message": "Field has to be unique.",
"extensions": {
"code": "RECORD_NOT_UNIQUE",
"collection": "test",
"field": null,
"invalid": "T1-D1",
"stack": "Error: Field has to be unique.\n at uniqueViolation (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\dialects\\mysql.ts:66:10)\n at extractError (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\dialects\\mysql.ts:22:11)\n at translateDatabaseError (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\translate.ts:25:24)\n at C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\services\\items.ts:134:39\n at processTicksAndRejections (node:internal/process/task_queues:96:5)"
}
}
]
} Error response for Postgres (has field): {
"errors": [
{
"message": "Field \"title, description\" has to be unique.",
"extensions": {
"code": "RECORD_NOT_UNIQUE",
"collection": "test",
"field": "title, description",
"invalid": "T1, D1",
"stack": "Error: Field \"title, description\" has to be unique.\n at uniqueViolation (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\dialects\\postgres.ts:46:9)\n at extractError (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\dialects\\postgres.ts:20:11)\n at translateDatabaseError (C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\exceptions\\database\\translate.ts:29:27)\n at C:\\Users\\Azri\\Documents\\Projects\\directus\\api\\src\\services\\items.ts:134:39\n at processTicksAndRejections (node:internal/process/task_queues:96:5)"
}
}
]
} This is how it looks like in Postgres for example: I believe this is shares the same root cause as #15033 (and it's also on MySQL), so let's track this issue over there. |
Granted this shouldn't be taken as an official solution as this logic is technically subject to change unless stated otherwise, you can try to name the constraint as directus/api/src/exceptions/database/dialects/mysql.ts Lines 46 to 50 in 0bffb2b
Thus for my example in the above comment, naming it as |
Thanks for the workaround, that works for now! :) |
Describe the Bug
We have a composite unique constraint on the database (created manually).
If we try to create an item on the app that violates the constraint, it logs an error 400 and ends in an empty items page form.
I know that directus doesn't support composite keys but I think the error could be catched and an error displayed. Directus at least notices that some unique constraint was violated, as on the Node console it logs an "Field has to be unique" error.
To Reproduce
1.) Create a collection with two fields
2.) Manually create a constraint that a combination of those two fields needs to be unique
3.) Create an item
4.) Try to create the same item again
Errors Shown
App: crashes, logs an error: "Failed to load resource: the server responded with a status of 400 (Bad Request)"
Node-process: Logs an error: "🐛 Field has to be unique."
What version of Directus are you using?
9.15.1
What version of Node.js are you using?
16.15
What database are you using?
MySql
What browser are you using?
Chrome
How are you deploying Directus?
N/A
The text was updated successfully, but these errors were encountered: