mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Added unique constraint notation to schema definition
refs https://github.com/TryGhost/Ghost/issues/12567 - Introduced here @@UNIQUE_CONSTRAINTS@@ notation allows to create unique contraints over multiple database fields. This will be needed to change posts' table unique constraint from `slug` to `slug+type`. - The notation is equivalent to SQL's: UNIQUE(column_name1,column_name2) - Example use in schema: posts: { slug: {type: 'string', maxlength: 191, nullable: false}, type: {type: 'string', maxlength: 50, nullable: false, defaultTo: 'post', validations: {isIn: [['post', 'page']]}}, '@@UNIQUES@@': [ ['slug', 'type'] ] }
This commit is contained in:
parent
7195a904ba
commit
6b61bcf123
2 changed files with 12 additions and 1 deletions
|
@ -104,6 +104,7 @@ function createTable(table, transaction, tableSpec = schema[table]) {
|
|||
|
||||
return (transaction || db.knex).schema.createTable(table, function (t) {
|
||||
let tableIndexes = [];
|
||||
let tableUniqueConstraints = [];
|
||||
|
||||
const columnKeys = _.keys(tableSpec);
|
||||
_.each(columnKeys, function (column) {
|
||||
|
@ -112,12 +113,21 @@ function createTable(table, transaction, tableSpec = schema[table]) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (column === '@@UNIQUE_CONSTRAINTS@@') {
|
||||
tableUniqueConstraints = tableSpec['@@UNIQUE_CONSTRAINTS@@'];
|
||||
return;
|
||||
}
|
||||
|
||||
return addTableColumn(table, t, column, tableSpec[column]);
|
||||
});
|
||||
|
||||
_.each(tableIndexes, function (index) {
|
||||
t.index(index);
|
||||
});
|
||||
|
||||
_.each(tableUniqueConstraints, function (unique) {
|
||||
t.unique(unique);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1254,7 +1254,8 @@ ghostBookshelf.Model = ghostBookshelf.Model.extend({
|
|||
|
||||
// exclude fields if enabled
|
||||
if (exclude) {
|
||||
const toSelect = _.keys(schema.tables[tableNames[modelName]]);
|
||||
let toSelect = _.keys(schema.tables[tableNames[modelName]]);
|
||||
toSelect = toSelect.filter(key => !(key.startsWith('@@')));
|
||||
|
||||
_.each(exclude, (key) => {
|
||||
if (toSelect.indexOf(key) !== -1) {
|
||||
|
|
Loading…
Add table
Reference in a new issue