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
Concrete type isn't written down in table inheritance #2544
Comments
I saw #184 I tried saving using the concrete repository and the correct type is written down. After that I can use the base repository to findX and that gets resolved to the correct type which I would think would be the most difficult thing. So is writing down the base type when using the base repository a bug? |
I encounter the same issue. Even storing a base entity using a child repo will set the child type to the base type, that's very unhandy when someone needs to store a set of data with mixed types. |
Hi! I have the same problem. I'm using cascade write to write Provider and its Resources in single request: Therea are multiple different Resource types available:
After persistance I see that every row at database is marked with I've tried to add explicit column Is there any working workaround avaliable? |
Same |
Hi,
With this, entities will be created not based on the type of the relation, which is the super-class, but based on the sub-class as defined by the given object itself. And thus the discriminating column in the single-table-model is set correctly. My little hack for sure needs some more testing and work (so that it will also work for relations without inheritance), but I hope that one of the developers might pick this up and fix this bug! And many thanks for this great ORM! |
Any progress here? |
Same here. I have been forced twice to find workarounds due to this problem. I see the issue dates back to 2018 and still hasn't been addressed. |
Its been 2 years and bug still seems to be there. Create file: import { EntityMetadata, ObjectLiteral } from "typeorm";
import { RelationMetadata } from "typeorm/metadata/RelationMetadata";
/*
Fix for wrong metadata being used when using table inheritance with cascade actions.
Read more: https://github.com/typeorm/typeorm/issues/2544
*/
function getInverseEntityMetadata(value: any, relation: RelationMetadata): EntityMetadata {
return relation.inverseEntityMetadata.childEntityMetadatas.find(metadata => {
return metadata.name == value.constructor.name;
})
// if no childEntityMetadata found return value that is used originaly by typeorm
?? relation.inverseEntityMetadata;
}
EntityMetadata.prototype.extractRelationValuesFromEntity = function(entity: ObjectLiteral, relations: RelationMetadata[]): [RelationMetadata, any, EntityMetadata][] {
// Exact copy of extractRelationValuesFromEntity method, except we calling getInverseEntityMetadata
// instead of using `relation.inverseEntityMetadata` which is incorrect for childEntity
const relationsAndValues: [RelationMetadata, any, EntityMetadata][] = [];
relations.forEach(relation => {
const value = relation.getEntityValue(entity);
if (Array.isArray(value))
value.forEach(subValue => relationsAndValues.push([relation, subValue, /* BUGFIX*/getInverseEntityMetadata(subValue, relation)]));
else if (value)
relationsAndValues.push([relation, value, /* BUGFIX*/getInverseEntityMetadata(value, relation)]);
});
return relationsAndValues;
};
|
@ZBAGI will you make a pull request with these changes? |
Issue type:
[X] question
[X] bug report
Database system/driver:
[X]
postgres
[X]
sqlite
TypeORM version:
[X]
0.2.7
(or put your version here)Steps to reproduce or a small repository showing the problem:
Maybe I'm doing something wrong; when I create a polymorphic container and save, writing down the entities the type written down for each entity is the supertype not the concrete type. Here's a snippet to reproduce, I also tried postgres with the same result
If you look at the logs and also inspect the database you'll see it's written down
Thing
as type notParticularThing
The text was updated successfully, but these errors were encountered: