question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Omitting @OneToMany decorator is valid?

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

export class User {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({
        unique: true
    })
    public email: string;

    @Column()
    public password: string;

    @ManyToOne(type => Home, home => home.id)
    public home: Home;

    @OneToMany(type => Electricity, electricity => electricity.home)
    public electricity: Electricity[];

    @OneToMany(type => Gas, gas => gas.home)
    public gas: Gas[];

    @OneToMany(type => Water, water => water.home)
    public water: Water[];
}
export class Meter {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column("decimal", { precision: 15, scale: 3 })
    public value: number;

    @Column()
    public date: Date;

    @ManyToOne(type => User, user => user.id)
    public user: User;

    @ManyToOne(type => Home, home => home.id)
    public home: Home;
}
export class Home {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column()
    public street: string;

    @Column()
    public town: string;

    @Column()
    public postalcode: number;

    @Column()
    public streetnumber: number;

    @OneToMany(type => User, user => user.id)
    public user: User[];

    @OneToMany(type => MeterCounter, counter => counter.id)
    public counter: MeterCounter[];

    @OneToMany(type => Electricity, electricity => electricity.home)
    public electricity: Electricity[];

    @OneToMany(type => Gas, gas => gas.home)
    public gas: Gas[];

    @OneToMany(type => Water, water => water.home)
    public water: Water[];
}

I got these Entities. The Meter Entity has two foreign keys: UserId and HomeId. It looks like I am creating a circular dependency as I get this error (Gas, Water and Electricity are just empty Entities which inherit from Meter):

export class Gas extends Meter { } TypeError: Class extends value undefined is not a constructor or null

However when I omit the @OneToMany decorator I don´t get the error and everything works fine. Is that a valid behaviour?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

11reactions
mnzakicommented, Aug 16, 2019

For future reference and googlers: #4190

You can now pass strings to the @OneToMany decorator:

@OneToMany('entity_name', 'attribute_name')
1reaction
cacabocommented, Jul 13, 2020

For future reference and googlers: #4190

You can now pass strings to the @OneToMany decorator:

@OneToMany('entity_name', 'attribute_name')

This fixes my OneToMany/ManyToOne relations, but for some reason does not fix the circular dep issue with OneToOne. Any ideas?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator reference - typeorm - GitBook
Marks a property in your entity as a table primary column. Same as @Column decorator but sets its primary option to true.
Read more >
nestjs - TypeORM: Joining when we have one to many and ...
You may also omit the @JoinColumn() decorators, as they are not necessary in one to many and many to one relationships, you can...
Read more >
Decorators | MikroORM
@Entity decorator is used to mark your model classes as entities. Do not use it for abstract base classes. Parameter, Type, Optional, Description....
Read more >
Association Mapping - Doctrine Object Relational Mapper ...
Likewise, inside the #[ManyToOne] attribute you can omit the targetEntity argument ... referencedColumnName: 'id', unique: true)] #[ManyToMany(targetEntity: ...
Read more >
Modeling Entity Relationships | MikroORM - GitHub Pages
When modeling bidirectional relationship, you can also omit the inversedBy ... Book implements IdEntity<Book> { @ManyToOne() // plain decorator is enough, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found