Skip to content

Commit

Permalink
fix(mysql): mark FK columns as unsigned for mixed composite PKs
Browse files Browse the repository at this point in the history
Closes #2844
  • Loading branch information
B4nan committed Mar 12, 2022
1 parent dde11d3 commit 67806cb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ export class MetadataDiscovery {

meta2.primaryKeys.forEach(primaryKey => {
const pk = meta2.properties[primaryKey];
prop.unsigned = this.platform.supportsUnsigned() && this.isNumericProperty(pk);
// if at least one of the target columns is unsigned, we need to mark the property as unsigned
prop.unsigned ||= this.platform.supportsUnsigned() && this.isNumericProperty(pk);
});

return;
Expand Down
68 changes: 68 additions & 0 deletions tests/features/schema-generator/GH2844.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Entity, Index, ManyToOne, MikroORM, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';

@Entity()
export class UserAction {

@PrimaryKey()
idUserAction!: string;

@Property()
name!: string;

}

@Entity()
@Index({ properties: ['id', 'userAction'] })
export class Step {

@PrimaryKey()
id!: number;

@ManyToOne({ primary: true, onDelete: 'cascade' })
userAction!: UserAction;

}

@Entity()
export class Component {

@PrimaryKey()
idComponent!: string;

@ManyToOne({ onDelete: 'cascade' })
step!: Step;

@OneToOne({
nullable: true,
fieldName: 'resultComponentId',
unique: false,
entity: () => Component,
})
resultComponent?: Component;

}

describe('complex FKs in mariadb (GH 2844)', () => {

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Component, Step, UserAction],
dbName: `mikro_orm_test_gh_2844`,
type: 'mariadb',
port: 3309,
});
await orm.getSchemaGenerator().ensureDatabase();
await orm.getSchemaGenerator().dropSchema();
});

afterAll(() => orm.close(true));

test('schema generator adds the m:1 columns and FK properly', async () => {
const sql = await orm.getSchemaGenerator().getCreateSchemaSQL();
expect(sql).toMatchSnapshot();
await orm.getSchemaGenerator().execute(sql);
});

});

0 comments on commit 67806cb

Please sign in to comment.