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

Invalid migration when adding a OneToOne #942

Closed
merceyz opened this issue Oct 21, 2020 · 2 comments
Closed

Invalid migration when adding a OneToOne #942

merceyz opened this issue Oct 21, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@merceyz
Copy link
Contributor

merceyz commented Oct 21, 2020

Describe the bug

Adding a OneToOne to an existing schema creates a invalid migration file that first adds the column then tries to add it again

Stack trace

DriverException: alter table `user` add column `profile_id` varchar not null; - SQLITE_ERROR: Cannot add a NOT NULL column with default value NULL
    at SqliteExceptionConverter.convertException (C:\repro\.yarn\$$virtual\@mikro-orm-core-virtual-a47c7ffdac\0\cache\@mikro-orm-core-npm-4.2.1-157e5819c7-1bb1c1779c.zip\node_modules\@mikro-orm\core\platforms\ExceptionConverter.js:8:16)
    at SqliteExceptionConverter.convertException (C:\repro\.yarn\$$virtual\@mikro-orm-sqlite-virtual-c27b82c743\0\cache\@mikro-orm-sqlite-npm-4.2.1-9331d09dc8-76e194b3f5.zip\node_modules\@mikro-orm\sqlite\SqliteExceptionConverter.js:46:22)
    at SqliteDriver.convertException (C:\repro\.yarn\$$virtual\@mikro-orm-core-virtual-a47c7ffdac\0\cache\@mikro-orm-core-npm-4.2.1-157e5819c7-1bb1c1779c.zip\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:168:54)
    at C:\repro\.yarn\$$virtual\@mikro-orm-core-virtual-a47c7ffdac\0\cache\@mikro-orm-core-npm-4.2.1-157e5819c7-1bb1c1779c.zip\node_modules\@mikro-orm\core\drivers\DatabaseDriver.js:172:24
    at Function.runSerial (C:\repro\.yarn\$$virtual\@mikro-orm-core-virtual-a47c7ffdac\0\cache\@mikro-orm-core-npm-4.2.1-157e5819c7-1bb1c1779c.zip\node_modules\@mikro-orm\core\utils\Utils.js:457:22)
    at C:\repro\.yarn\$$virtual\@mikro-orm-migrations-virtual-5fb7d99af2\0\cache\@mikro-orm-migrations-npm-4.2.1-510440861c-2f6443fc3a.zip\node_modules\@mikro-orm\migrations\MigrationRunner.js:23:17

To Reproduce

import 'reflect-metadata';
import { Entity, MikroORM, PrimaryKey, OneToOne } from '@mikro-orm/core';

async function createAndRunMigration(entities: any[]) {
	const db = await MikroORM.init({
		type: 'sqlite',
		entities: entities,
		dbName: __dirname + '/db.sqlite',
		migrations: {
			path: __dirname + '/migrations',
		},
	});

	await db.getMigrator().createMigration();
	await db.getMigrator().up();

	await db.close();
}

@Entity({ tableName: 'profile' })
class Profile {
	@PrimaryKey()
	id: string;
}

@Entity({ tableName: 'user' })
class User {
	@PrimaryKey()
	id: string;
}

@Entity({ tableName: 'user' })
class User2 {
	@PrimaryKey()
	id: string;

	@OneToOne(() => Profile)
	profile: Profile;
}

(async () => {
	await createAndRunMigration([User, Profile]);

	await new Promise((resolve) => {
		setTimeout(resolve, 2000);
	});

	// Simulates adding `profile` to the User entity
	await createAndRunMigration([User2, Profile]);
})();

Expected behavior

The migration to be valid or an error to be thrown if i'm configuring the relationships incorrectly

Additional context

The migration created

this.addSql('alter table `user` add column `profile_id` varchar not null;');
this.addSql('create index `user_profile_id_index` on `user` (`profile_id`);');
this.addSql('create unique index `user_profile_id_unique` on `user` (`profile_id`);');

this.addSql('alter table `user` add column `profile_id` varchar null;');
this.addSql('create index `user_profile_id_index` on `user` (`profile_id`);');
this.addSql('create unique index `user_profile_id_unique` on `user` (`profile_id`);');

Versions

Dependency Version
node v12.19.0
typescript 4.0.3
@mikro-orm/core 4.2.1
@mikro-orm/migrations 4.2.1
@mikro-orm/sqlite 4.2.1
@B4nan
Copy link
Member

B4nan commented Oct 21, 2020

I don't think this is a good way to simulate adding a property. You have two entities loaded, the dump looks exactly like that - 3 statements for each entity. Each entity is checked for diff, there is no grouping based on table name. But the output is weird for sure...

edit: I see, now I get it, you register only the User2 for the second time...

@B4nan B4nan added the bug Something isn't working label Oct 21, 2020
@B4nan B4nan closed this as completed in 1eb6374 Oct 22, 2020
@merceyz
Copy link
Contributor Author

merceyz commented Oct 22, 2020

Can confirm that fixed it in the real project 🎉 , thanks for the quick fix 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants