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

Fails to discover abstract classes through EntitySchema #4080

Closed
tverstraten opened this issue Feb 28, 2023 · 8 comments
Closed

Fails to discover abstract classes through EntitySchema #4080

tverstraten opened this issue Feb 28, 2023 · 8 comments

Comments

@tverstraten
Copy link

tverstraten commented Feb 28, 2023

Fails to discover abstract classes through EntitySchema (BaseClass and BaseInterface)

Stack trace
none

To Reproduce

import { EntitySchema, MikroORM } from "@mikro-orm/core";
import { MySqlDriver } from "@mikro-orm/mysql";

abstract class BaseClass {
	id?: number;
}
interface BaseInterface {
	id?: number;
}
class DerivedClass extends BaseClass {
	name?: string;
}
class ImplementingClass implements BaseInterface {
	id?: number;
	name?: string;
}
const BaseClassSchema = new EntitySchema<BaseClass>({
	name: "BaseClass",
	abstract: true,
	properties: {
		id: { type: Number, primary: true },
	},
});
const BaseInterfaceSchema = new EntitySchema<BaseInterface>({
	name: "BaseInterface",
	abstract: true,
	properties: {
		id: { type: Number, primary: true },
	},
});

const DerivedClassSchema = new EntitySchema<DerivedClass>({
	class: DerivedClass,
	extends: "BaseClass",
	properties: {
		name: { type: String },
	},
});
const ImplementingClassSchema = new EntitySchema<ImplementingClass>({
	class: ImplementingClass,
	extends: "BaseInterface",
	properties: {
		name: { type: String },
	},
});
async function test() {
	var pool = await MikroORM.init<MySqlDriver>({
		entities: [
			BaseClassSchema,
			DerivedClassSchema,
			BaseInterfaceSchema,
			ImplementingClassSchema,
		],
		type: "mysql",
		host: "localhost",
		dbName: "community",
		user: "root",
		password: "doesnt matter",
		debug: true,
	});
}

test();

the output is

[info] MikroORM version: 5.6.12
[discovery] ORM entity discovery started, using ReflectMetadataProvider
[discovery] - processing entity **undefined**
[discovery] - processing entity DerivedClass
[discovery] - processing entity **undefined**
[discovery] - processing entity ImplementingClass
[discovery] - entity discovery finished, found 4 entities, took 13 ms

Expected behavior
both BaseClass and BaseInterface should be discovered

Versions

Dependency Version
node v16.16.0
typescript 4.9.5
mikro-orm 5.6.12
mysql ?5.6.12
@B4nan
Copy link
Member

B4nan commented Feb 28, 2023

Looks more like a logging issue, or do you see something malfunctioning too, apart from the undefined in your logs? The properties in the final metadata are correct, they include both id and name. Abstract classes won't have any metadata after discovery, it gets inlined to the implementing classes.

@tverstraten
Copy link
Author

tverstraten commented Feb 28, 2023 via email

@B4nan
Copy link
Member

B4nan commented Feb 28, 2023

Ok, can you provide a reproduction that fails? As the one you provided works fine for me, I am able to create entities as well as query them.

Sounds like you want to query by a base class, which is indeed not supported, as that does not make much sense, you need to query for a db table and a base class does not represent one.

@tverstraten
Copy link
Author

tverstraten commented Feb 28, 2023 via email

@B4nan
Copy link
Member

B4nan commented Feb 28, 2023

As I already said above, you can't query by base class, that does not make any sense.

@tverstraten
Copy link
Author

tverstraten commented Feb 28, 2023 via email

@B4nan
Copy link
Member

B4nan commented Feb 28, 2023

Once again, this is not possible, you can't query by it, neither you can target it in a relation - as it does not represent any database table.

@B4nan
Copy link
Member

B4nan commented Feb 28, 2023

@B4nan B4nan closed this as completed in e721ad7 Feb 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants