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

MissingPrimaryColumnError when primary defined in relation #43

Closed
MichalLytek opened this issue Sep 25, 2016 · 5 comments
Closed

MissingPrimaryColumnError when primary defined in relation #43

MichalLytek opened this issue Sep 25, 2016 · 5 comments

Comments

@MichalLytek
Copy link
Contributor

MichalLytek commented Sep 25, 2016

I have an entity described that:

import { Test } from "./test";
import { Product } from "./product";

import { Table, Column, ManyToOne } from "typeorm";

@Table()
export class Result {
    @ManyToOne(type => Test, { primary: true, nullable: false })
    public test: Test;

    @ManyToOne(type => Product, { primary: true, nullable: false })
    public product: Product;

    @Column()
    public primaryValue: number;

    @Column()
    public secondaryValue: number;
}

And I have an error on launch:
Error! { MissingPrimaryColumnError: Entity "Result" does not have a primary column. Primary column is required to have in all your entities. Use @PrimaryColumn decorator to add a primary column to your entity.

Does the primary in relations option doesn't work yet or it's just a bug in checking if primary is defined?

@MichalLytek MichalLytek changed the title when primary defined in relation MissingPrimaryColumnError when primary defined in relation Sep 25, 2016
@pleerock
Copy link
Member

this is not supported yet. But it should be in the future version. Right now its a bit complicated, because orm doesn't analize insertion/updating order (which is complicated) and first inserts object with entity with NULL in relationId, then updates its relationId after it saves related entity. With relation's column primary key it won't work because primary key cannot be NULL.

So, to fix this issue it needs deep refactoring, and I'll plan it to the future versions

@MichalLytek
Copy link
Contributor Author

Ok, it's not a big deal for now - I created PrimaryGeneratedColumn to have a foreign key for my other relation.

But is there an option to have a pair of relation fields defined as an unique constraint?
I need to have only one result for a pair of Test and Product.

@pleerock
Copy link
Member

maybe using indices can help?

@MichalLytek
Copy link
Contributor Author

Yes, thanks! It works great now 😄 Sorry for asking this kind of question but the docs is empty and website documentation was updated today and I haven't checked it yet 😉

@rostamiani
Copy link

maybe using indices can help?

Excuse me, How indices can help with this error?
I added @Index() to both keys and nothing solved

(Your link is no longer valid)

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

3 participants