mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 08:08:05 +00:00
Remove FK on many-to-many relation before drop the table
This commit is contained in:
parent
1c6a96fd2c
commit
40267a770d
@ -132,23 +132,44 @@ module.exports = function (models, modelName, details, attribute, toDrop, onlyDr
|
|||||||
// Save the relation table as a new model in the scope
|
// Save the relation table as a new model in the scope
|
||||||
// aiming to benefit of templates for the table such as
|
// aiming to benefit of templates for the table such as
|
||||||
// `createTableIfNotExists` and `dropTable`.
|
// `createTableIfNotExists` and `dropTable`.
|
||||||
models[relationTable] = {};
|
|
||||||
|
|
||||||
// Template: create the table for the `up` export if it doesn't exist.
|
// Template: create the table for the `up` export if it doesn't exist.
|
||||||
// This adds a `up` logic for the relation table.
|
// This adds a `up` logic for the relation table.
|
||||||
const tplTableUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsToMany.template'), 'utf8');
|
const tplTableUp = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'relations', 'belongsToMany.template'), 'utf8');
|
||||||
models[relationTable].up = _.unescape(_.template(tplTableUp)({
|
_.set(models, relationTable + '.up.others', _.unescape(_.template(tplTableUp)({
|
||||||
models: models,
|
models: models,
|
||||||
tableName: relationTable,
|
tableName: relationTable,
|
||||||
details: details,
|
details: details,
|
||||||
relationship: relationship
|
relationship: relationship
|
||||||
}));
|
})));
|
||||||
|
|
||||||
// Template: drop the table for the `down` export.
|
// Template: drop the table for the `down` export.
|
||||||
// This adds a `down` logic for the relation table.
|
// This adds a `down` logic for the relation table.
|
||||||
const tplTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'dropTable.template'), 'utf8');
|
const tplTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'dropTable.template'), 'utf8');
|
||||||
models[relationTable].down = _.unescape(_.template(tplTableDown)({
|
_.set(models, relationTable + '.down.others', _.unescape(_.template(tplTableDown)({
|
||||||
tableName: relationTable
|
tableName: relationTable
|
||||||
|
})));
|
||||||
|
|
||||||
|
const tplFKDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'columns', 'dropForeign.template'), 'utf8');
|
||||||
|
const tplSelectTableDown = fs.readFileSync(path.resolve(__dirname, '..', '..', 'templates', 'builder', 'tables', 'select', 'down.template'), 'utf8');
|
||||||
|
|
||||||
|
_.set(models, relationTable + '.attributes', {
|
||||||
|
fk: {
|
||||||
|
delete: {
|
||||||
|
drop: _.unescape(_.template(tplFKDown)({
|
||||||
|
attribute: details.attribute + '_' + details.column
|
||||||
|
})) + _.unescape(_.template(tplFKDown)({
|
||||||
|
attribute: relationship.attribute + '_' + relationship.column
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
models[relationTable].down.drop = _.unescape(_.template(tplSelectTableDown)({
|
||||||
|
models: models,
|
||||||
|
tableName: relationTable,
|
||||||
|
attributes: models[relationTable].attributes,
|
||||||
|
toDrop: true
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
|
|
||||||
// Delete the `<%= attribute %>` column.
|
// Delete the `<%= attribute %>` column with unique constraint.
|
||||||
table.dropUnique('<%= attribute %>').dropColumn('<%= attribute %>')
|
table.dropUnique('<%= attribute %>').dropColumn('<%= attribute %>')
|
||||||
|
|||||||
@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
// Delete the foreign key on `<%= attribute %>` column.
|
||||||
|
table.dropForeign('<%= attribute %>')
|
||||||
Loading…
x
Reference in New Issue
Block a user