Skip to content

Model strict: false is ignored with Postgre SQL repository #4042

Description

@shreking-around

Hello,

I have a problem with class inheritance and repository using Postgre SQL.

Let's say we have Animal parent class and then Dog and Chicken child classes. I store them in one table (Table Per Hierarchy Inheritance) called animal.

In LB4 App I have Animal base model (entityType is a discriminator):

@model({
    settings: {
        postgresql: {
            table: 'animal',
        },
        idInjection: false,
        strict: false,
    },
})
export class Animal extends Entity {
    @property({
        type: 'string',
        id: true,
        postgresql: {
            dataType: 'uuid',
        },
    })
    id: string;

    @property({
        type: 'string',
        required: true,
    })
    entityType: string;
 
    @property({
        type: 'number',
        required: true,
    })
    weight: number;

    @hasMany(() => Zookeeper)
    zookepers?: Zookeeper[];
 

    constructor(data?: Partial<Animal>) {
        super(data);
    }
}

and child class dog

@model()
export class Dog extends Animal {
 
    @property({
        type: 'number',
        required: true

    })
    barkingLevel: number;

    constructor(data?: Partial<Dog>) {
        super(data);
    }
}

When I query a dog from the repository barkingLevel is ommited on output though the strict is set to false.

This will return animal (not a dog) without barkingLevel .

 const dog : Dog = await this.animalRepository.findById(id, {
            include: [
                { relation: 'zookepers' }
            ],
        }) as Dog;

Repository:

export class AnimalRepository extends DefaultCrudRepository<
    Animal,
    typeof Animal.prototype.id
> {
    //declare has-many repositories for related zookeeper entities
    public readonly zookeepers: HasManyRepositoryFactory<
        Zookeeper,
        typeof Zookeeper.prototype.id
    >;

....

Expected result: Returned animal from repository should include extra properties from dog since the strict setting is set to false.

Current result: Only properties belonging to parent class are returned.

NOTE: If I do the same thing in Mongo DB, everything works as expected.

Thank you.

Acceptance criteria

See #4042 (comment)

At the moment, the data is discarded silently, the framework does not print any warnings about unsupported strict value used with a SQL connector. Personally, I'd like the framework to detect this scenario and print a warning (once for each model), so that developers have easier time troubleshooting the unexpected behavior.

Metadata

Metadata

Assignees

Labels

db:PostgreSQLTopics specific to PostgreSQLdeveloper-experienceIssues affecting ease of use and overall experience of LB usersfeature

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions