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

OneToMany sub-relations query return empty properties object #3657

Closed
anthonylau opened this issue Feb 17, 2019 · 3 comments
Closed

OneToMany sub-relations query return empty properties object #3657

anthonylau opened this issue Feb 17, 2019 · 3 comments
Labels

Comments

@anthonylau
Copy link

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.13

Steps to reproduce or a small repository showing the problem:

Models

@Entity()
export class Foo {
    @PrimaryGeneratedColumn()
    public id?: number;

    @OneToMany(type => FooBar, foobar => foobar.foo)
    public fooBars?: FooBar[];
}

@Entity()
export class Bar {
    @PrimaryGeneratedColumn()
    public id?: number;

    @OneToMany(type => FooBar, foobar => foobar.foo)
    public fooBars?: FooBar[];
}

@Entity()
export class FooBar {
    @ManyToOne(type => Foo, foo => foo.fooBars, { primary: true })
    public foo: Foo | null = null;

    @ManyToOne(type => Bar, bar => bar.fooBars, { primary: true })
    public bar: Bar | null = null;
}

Data & Query

const mgr = queryRunner.manager;

const foo = new Foo();
await mgr.save(foo);

const result = await mgr.find(Foo, { relations: ['fooBars', 'fooBars.bar'] });
console.log(JSON.stringify(result, null, 4));

Actual Result

[
    {
        "id": 1,
        "fooBars": [
            {
                "foo": null,
                "bar": null
            }
        ]
    }
]

Expected Result

[
    {
        "id": 1,
        "fooBars": []
    }
]
@Kononnable
Copy link
Contributor

You can get it working by specyfying FooBar entity a bit different:

@Entity()
export class FooBar {
    @PrimaryColumn()
    public fooid?: number;

    @PrimaryColumn()
    public barid?: number;

    @ManyToOne(type => Foo, foo => foo.fooBars)
    @JoinColumn({ name: "fooid"})
    public foo: Foo | null = null;

    @ManyToOne(type => Bar, bar => bar.fooBars)
    @JoinColumn({ name: "barid"})
    public bar: Bar | null = null;
}

Also in 0.3.0 defining a primary column will be required for primary keyed relations - #3661 (comment)

@anthonylau
Copy link
Author

It works. Thanks @Kononnable

@sergeyrudenko
Copy link

I can't run migration with this method, maybe someone know another solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants