Skip to content
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

feat: ensure ID is primary key after autoupdate #460

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,20 @@ function mixinMigration(MySQL, mysql) {
const sql = [];

propNames.forEach(function(propName) {
if (m.properties[propName] && self.id(model, propName)) return;
// If the field is set as an ID inside the model
if (m.properties[propName] && self.id(model, propName)) {
const existingIdField = actualFields?.find(
({ Field }) => Field === expectedColNameForModel(propName, m)
);

// And the same field is already present inside the DB, but not set as a
// primary key
if (existingIdField && existingIdField.Key !== 'PRI') {
// Set the field to а primary key
sql.push(`ADD PRIMARY KEY (\`${existingIdField.Field}\`)`);
} else { return; }
};

let found;
const colName = expectedColNameForModel(propName, m);
if (actualFields) {
Expand Down