Composite key support #12137
Replies: 6 comments 10 replies
-
I would like to see unique composite keys. For example: A user can set allergic markings, which should not be dublicate in the junction table |
Beta Was this translation helpful? Give feedback.
-
How would I create the constraint in the GUI?
Beste Grüße,
Nils Baumgartner
… Am 28.06.2022 um 19:49 schrieb Gerard Lamusse ***@***.***>:
The easiest and simplest way to do this is adding a constraint on the two columns that should be unique.
The reason a single will always be preferred over two is both for navigation (CRUD) purposes as well as keeping things simple.
If you want here is a simple migration for creating a composite index on two columns (OPTIONALLY MORE).
// extensions/migrations/20220625-somethings-composite-index.js
module.exports = {
async up(knex) {
await knex.schema.table('somethings', (table) => {
table.unique(['thing', 'other']);
});
},
async down(knex) {
await knex.schema.table('somethings', (table) => {
table.dropUnique(['thing', 'other']);
});
},
};
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.
|
Beta Was this translation helpful? Give feedback.
-
I needed a composite unique key across two M2M fields. To get around this missing feature I ended up writing a filter() hook implementation. Here's some example code:
// a custom re-implementation of the Directus RecordNotUniqueException; see below.
import { RecordNotUniqueException } from './RecordNotUniqueException';
export default ({ filter }, { exceptions, services }) => {
const { ItemsService } = services;
filter('assignment.items.create', async (payload, collection, ctx) => {
const { accountability, schema } = ctx;
const Assignment = new ItemsService('assignment', { schema: schema, accountability: accountability });
// get the two M2M field values from the incoming payload
const {item_id, profile_id} = payload;
// do we already have an existing [profile]>-<[item] combination?
const assignment = await Assignment.readByQuery({
filter: {
item_id: {
_eq: item_id,
},
profile_id: {
_eq: profile_id,
},
},
});
// stop the creation of the record if an existing [profile_id]>-<[item_id] record already exists
if(typeof assignment[0] !== 'undefined'){
throw new RecordNotUniqueException('[item]>-<[profile]]');
}
});
};
// @SOURCE: node_modules/directus/dist/exceptions/database/record-not-unique.js
import { BaseException } from '@directus/shared/exceptions';
class RecordNotUniqueException extends BaseException {
constructor(field, extensions) {
if (field) {
super(`Field "${field}" has to be unique.`, 400, 'RECORD_NOT_UNIQUE', extensions);
}
else {
super(`Field has to be unique.`, 400, 'RECORD_NOT_UNIQUE', extensions);
}
}
}
export { RecordNotUniqueException }; |
Beta Was this translation helpful? Give feedback.
-
We created a gist explaining how we did something similar using Flows: https://gist.github.com/Natetronn/9bafd1722a2b67b9bf4528e45992baa2 Note: it may be a bit rough around the edges and probably not the best method of achieving this, but perhaps it can be of use to others, just the same. |
Beta Was this translation helpful? Give feedback.
-
Heya! Thanks for opening this feature request! This feature request has received over 15 votes from the community. This means we'll move this feature request to the Under Review state! The Core team will schedule a meeting to review this request as soon as possible. The discussion will then be approved or denied. You may or may not be invited to join this meeting with the core team. For more information, see our Feature Request Process. |
Beta Was this translation helpful? Give feedback.
-
As a simple workaround we have created key constraints on two columns being unique via a migration. This does throw and error in the admin dashboard if we attempt to create a record with two keys that are already paired.
|
Beta Was this translation helpful? Give feedback.
-
Hello,
A really missing functionality support is the composite management. Directus doesn't permit to create a unique composite key from GUI and even we create the composite key from SQL command (with
ALTER TABLE <table name> ADD UNIQUE <key name>(<indexes>);
), no errors are thrown...Beta Was this translation helpful? Give feedback.
All reactions