Fixed issue with bigincrements not working with composite primary key in MySQL - #5341 (#5343)

Co-authored-by: David Farrugia <david.farrugia@sagossgroup.com>
This commit is contained in:
davidf84 2023-11-29 01:10:12 +01:00 committed by GitHub
parent 0a29f6c5d0
commit 770b2f20ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View File

@ -160,6 +160,14 @@ class TableCompiler_MySQL extends TableCompiler {
}
});
}
const bigIncrementsCols = this._getBigIncrementsColumnNames();
if (bigIncrementsCols.length) {
bigIncrementsCols.forEach((c) => {
if (!columns.includes(c)) {
columns.unshift(c);
}
});
}
}
return `,${constraintName} primary key (${this.formatter.columnize(
@ -292,6 +300,7 @@ class TableCompiler_MySQL extends TableCompiler {
const primaryCols = columns;
let incrementsCols = [];
let bigIncrementsCols = [];
if (this.grouped.columns) {
incrementsCols = this._getIncrementsColumnNames();
if (incrementsCols) {
@ -301,6 +310,14 @@ class TableCompiler_MySQL extends TableCompiler {
}
});
}
bigIncrementsCols = this._getBigIncrementsColumnNames();
if (bigIncrementsCols) {
bigIncrementsCols.forEach((c) => {
if (!primaryCols.includes(c)) {
primaryCols.unshift(c);
}
});
}
}
if (this.method !== 'create' && this.method !== 'createIfNot') {
this.pushQuery(
@ -316,6 +333,13 @@ class TableCompiler_MySQL extends TableCompiler {
)} int unsigned not null auto_increment`
);
}
if (bigIncrementsCols.length) {
this.pushQuery(
`alter table ${this.tableName()} modify column ${this.formatter.columnize(
bigIncrementsCols
)} bigint unsigned not null auto_increment`
);
}
}
unique(columns, indexName) {

View File

@ -418,6 +418,12 @@ class TableCompiler {
.filter((c) => c.builder._type === 'increments')
.map((c) => c.builder._args[0]);
}
_getBigIncrementsColumnNames() {
return this.grouped.columns
.filter((c) => c.builder._type === 'bigincrements')
.map((c) => c.builder._args[0]);
}
}
TableCompiler.prototype.pushQuery = pushQuery;

View File

@ -723,6 +723,25 @@ module.exports = function (dialect) {
);
});
it('test basic create table with composite key on big incrementing column + other', function () {
tableSql = client
.schemaBuilder()
.createTable('users', function (table) {
table.primary(['userId', 'name']);
table.bigincrements('userId');
table.string('name');
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal(
'create table `users` (`userId` bigint unsigned not null, `name` varchar(255), primary key (`userId`, `name`))'
);
expect(tableSql[1].sql).to.equal(
'alter table `users` modify column `userId` bigint unsigned not null auto_increment'
);
});
it('test adding column after another column', function () {
tableSql = client
.schemaBuilder()