-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(mysql): fix reloading of database defaults for complex composite PKs
Closes #3965
- Loading branch information
Showing
7 changed files
with
126 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Collection, Entity, Property, ManyToOne, OneToMany, PrimaryKey, PrimaryKeyType, Cascade, Ref } from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/mysql'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
@Entity() | ||
export class Category { | ||
|
||
@PrimaryKey({ | ||
length: 36, | ||
}) | ||
id!: string; | ||
|
||
@Property({ | ||
defaultRaw: 'CURRENT_TIMESTAMP', | ||
}) | ||
createdAt?: Date; | ||
|
||
@OneToMany(() => Article, attr => attr.category, { | ||
cascade: [Cascade.ALL], | ||
}) | ||
articles = new Collection<Article>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
export class Article { | ||
|
||
@PrimaryKey({ | ||
length: 36, | ||
}) | ||
id!: string; | ||
|
||
@Property({ | ||
defaultRaw: 'CURRENT_TIMESTAMP', | ||
}) | ||
createdAt?: Date; | ||
|
||
@ManyToOne(() => Category, { | ||
primary: true, | ||
ref: true, | ||
}) | ||
category!: Ref<Category>; | ||
|
||
[PrimaryKeyType]?: [string, string]; | ||
|
||
@OneToMany(() => ArticleAttribute, attr => attr.article, { | ||
cascade: [Cascade.ALL], | ||
}) | ||
attributes = new Collection<ArticleAttribute>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
export class ArticleAttribute { | ||
|
||
@PrimaryKey({ length: 36 }) | ||
id!: string; | ||
|
||
@Property({ | ||
defaultRaw: 'CURRENT_TIMESTAMP', | ||
}) | ||
createdAt?: Date; | ||
|
||
@ManyToOne(() => Article, { | ||
primary: true, | ||
ref: true, | ||
}) | ||
article!: Ref<Article>; | ||
|
||
[PrimaryKeyType]?: [string, [string, string]]; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: 'mikro_orm_3965', | ||
entities: [Article], | ||
port: 3308, | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('3965', async () => { | ||
const category = new Category(); | ||
category.id = randomUUID(); | ||
|
||
const article = new Article(); | ||
article.id = randomUUID(); | ||
|
||
category.articles.add(article); | ||
|
||
const articleAttribute = new ArticleAttribute(); | ||
articleAttribute.id = randomUUID(); | ||
|
||
article.attributes.add(articleAttribute); | ||
expect(category.createdAt).toBeUndefined(); | ||
await orm.em.persistAndFlush(category); | ||
expect(category.createdAt).toBeDefined(); | ||
expect(category.createdAt).toBeInstanceOf(Date); | ||
}); |