Skip to content

Commit 3298cd4

Browse files
Stefan BlaginovStefan Blaginov
Stefan Blaginov
authored and
Stefan Blaginov
committed
feat: ensure ID is primary key after autoupdate
1 parent 3da25ae commit 3298cd4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/migration.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,20 @@ function mixinMigration(MySQL, mysql) {
204204
const sql = [];
205205

206206
propNames.forEach(function(propName) {
207-
if (m.properties[propName] && self.id(model, propName)) return;
207+
// If the field is set as an ID inside the model
208+
if (m.properties[propName] && self.id(model, propName)) {
209+
const existingIdField = actualFields?.find(
210+
({ Field }) => Field === expectedColNameForModel(propName, m)
211+
);
212+
213+
// And the same field is already present inside the DB, but not set as a
214+
// primary key
215+
if (existingIdField && existingIdField.Key !== 'PRI') {
216+
// Set the field to а primary key
217+
sql.push(`ADD PRIMARY KEY (\`${existingIdField.Field}\`)`);
218+
} else { return; }
219+
};
220+
208221
let found;
209222
const colName = expectedColNameForModel(propName, m);
210223
if (actualFields) {

0 commit comments

Comments
 (0)