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

Omitting @OneToMany decorator is valid? #2059

Closed
MeMeMax opened this issue May 1, 2018 · 24 comments
Closed

Omitting @OneToMany decorator is valid? #2059

MeMeMax opened this issue May 1, 2018 · 24 comments

Comments

@MeMeMax
Copy link

MeMeMax commented May 1, 2018

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?

@pleerock
Copy link
Member

pleerock commented May 1, 2018

Omitting @onetomany decorator is valid?

yes it is valid, you can omit it if you don't need it.

@MeMeMax
Copy link
Author

MeMeMax commented May 1, 2018

Could I face any problems by ommiting these?

@pleerock
Copy link
Member

pleerock commented May 1, 2018

No

@MeMeMax
Copy link
Author

MeMeMax commented May 2, 2018

Then this should be a bug, right? If I can just omit it to solve the error? Or on the other hand: Could I solve the error without omitting it?

@pleerock
Copy link
Member

pleerock commented May 2, 2018

circular reference issue isn't a typeorm issue. Try to redesign your classes to avoid this issue.

@cjackson234
Copy link

@pleerock, how should this be designed to avoid this error? Can you do something like

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

and it still is valid?

@pleerock
Copy link
Member

pleerock commented May 4, 2018

I can help if you provide a minimal reproduction repo

@ghost
Copy link

ghost commented Jul 11, 2018

@pleerock from the README:

Author contains an inverse side of a relation. OneToMany is always an inverse side of relation, and it can't exist without ManyToOne on the other side of the relation.

This seems to state that circular references are not only encouraged, they're required.

@pleerock
Copy link
Member

@errorx666 correct, because bi-directional relation is circular by nature.

@ghost
Copy link

ghost commented Jul 11, 2018

So this feature is just not supported with ECMAScript modules/webpack, as they don't allow this?

@pleerock
Copy link
Member

ECMAScript modules does not cover it right? What about webpack, I don't know, it might throw warnings, but it will still work. And it is working in node if we talk about node.

@ghost
Copy link

ghost commented Jul 11, 2018

I get the Foo is not defined errors with node using webpack awesome-typescript-loader (es2017 target) + babel-loader.

@pleerock
Copy link
Member

you use webpack on the backend?

@ghost
Copy link

ghost commented Jul 11, 2018

Correct.

@pleerock
Copy link
Member

maybe some specifics of tools you are using. Try to provide inline require, e.g.
@OneToMany(type => require("./Gas"), ...)

@ghost
Copy link

ghost commented Jul 11, 2018

That had the same problem. Changing my @babel/preset-env config to use modules: 'commonjs' instead of modules: false worked.

@atul221282
Copy link

How can this bug be closed? There is no resolution provided and it is still hapening

@Kononnable
Copy link
Contributor

  1. I think this is more of a question then a bug in typeorm(it's even reported as such).
  2. One post above there is a resolution - after changing other tool configuration setting problem disappeared.

If you have similar problem ask on slack or create new issue, but in both cases provide minimal reproduction data - otherwise we won't have enough information to be able to help.

@ghost
Copy link

ghost commented May 2, 2019

The problem is basically with ESM, because circular references give null values. Using commonjs instead of ESM is the workaround.

I still think typeorm should support ESM.

@pleerock
Copy link
Member

pleerock commented May 3, 2019

I think there is no way to resolve current circular references issue that happens in some particular cases using decorators. Solution might be to use () => require("..") in the references or don't use decorators and use entity schemas instead. One another reason why I want to make entity schemas as primary schema definition source in the future ORM versions.

@mnzaki
Copy link

mnzaki commented Aug 16, 2019

For future reference and googlers: #4190

You can now pass strings to the @OneToMany decorator:

@OneToMany('entity_name', 'attribute_name')

@nbaua
Copy link

nbaua commented Dec 29, 2019

ECMAScript modules does not cover it right? What about webpack, I don't know, it might throw warnings, but it will still work. And it is working in node if we talk about node.

It doesn't actually.. I get null values
I've created a minimal repo for this issue: Guys feel free to contribute and lets resolve this in best possible way. (mysql script also included for those who wants to fork/clone and check it further)

https://github.com/nbaua/nest-nx-typeorm-circular-dep

@nbaua
Copy link

nbaua commented Jan 1, 2020

I can help if you provide a minimal reproduction repo

https://github.com/nbaua/nest-nx-typeorm-circular-dep

@cacabo
Copy link

cacabo commented 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?

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

8 participants