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.

ManyToOne relation with custom column name

See original GitHub issue

I’m trying to get setup an example for my mysql-db with real customer data (and tables). Two simple tables called artikel and kollektion have a simple may-to.one relation (many articles may be part of one collection).

`

@Table(“artikel”) export class Artikel {

@PrimaryColumn("int", { generated: true })
@Column({name: "artikel_id"})
id: number;

@Column({name: "artikel_nummer"})
nummer: string;

@Column({name: "artikel_name"})
name: string;

@Column({name: "artikel_extrabarcode"})
extrabarcode: string;

@Column({name: "artikel_saison"})
saison: string;

@ManyToOne(type => Kollektion, { cascadeAll: true })
@Column("int", {name: "id_kollektion"})
kollektion: Kollektion;

}

@Table(“kollektion”) export class Kollektion {

@PrimaryColumn("int", { generated: true })
@Column({name: "kollektion_id"})
id: number;

@Column({name: "kollektion_name"})
name: string;

}

`

In Table Artikel the FK column to table kollektion is named ‘id_kollektion’ and not ‘artikel’. I tried to specify the different column as shown above by adding a ‘@Column(“int”, {name: “id_kollektion”})’ but when executing a persist I get the following error: ... executing query: START TRANSACTION executing query: INSERT INTO artikel(artikel_nummer, artikel_name, artikel_saison, id_kollektion, kollektion) VALUES ( ?,?,?,?,?) -- PARAMETERS: ["SNB-001","Snowboard WL","winter-2016",{"name":"Burton 2016"},null] executing query: INSERT INTO kollektion(kollektion_name) VALUES (?) -- PARAMETERS: ["Burton 2016"] query failed: INSERT INTO artikel(artikel_nummer, artikel_name, artikel_saison, id_kollektion, kollektion) VALUES (?,? ,?,?,?) -- PARAMETERS: ["SNB-001","Snowboard WL","winter-2016",{"name":"Burton 2016"},null] executing query: ROLLBACK Cannot save. Error: { Error: ER_BAD_FIELD_ERROR: Unknown column 'kollektion' in 'field list' ...

Seems that there is no support for specifying a custom column name. I think this would be a necessary feature for accessing existing database schemas.

Regards, Walter

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
pleerockcommented, Nov 26, 2016

there are other errors as well. Here is correct code:

@Table("artikel")
export class Artikel {

    @PrimaryColumn("int", { generated: true, name: "artikel_id" })
    id: number;

    @Column({ name: "artikel_nummer" })
    nummer: string;

    @Column({ name: "artikel_name" })
    name: string;

    @Column({ name: "artikel_extrabarcode" })
    extrabarcode: string;

    @Column({ name: "artikel_saison" })
    saison: string;

    @ManyToOne(type => Kollektion, { cascadeAll: true })
    @JoinColumn({ name: "id_kollektion" })
    kollektion: Kollektion;

}

@Table("kollektion")
export class Kollektion {

    @PrimaryColumn("int", { generated: true, name: "kollektion_id" })
    id: number;

    @Column({ name: "kollektion_name" })
    name: string;

}
0reactions
WalterLeinertcommented, Nov 26, 2016

Hi pleerock, Thank you for your corrections and hints. I will retest the issue and close it if it works as expected.

Regards, Walter

Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA: default column name mapping for @ManyToOne relations
When we have a class: @Entity Order implements Serializable { @Id private Integer id; ... } and: @Entity OrderLine implements Serializable { @Id ......
Read more >
Many To One Set Join Column Name - Java Tutorial
1. Mapping Relationship State · 2. Many To One Set Join Column Name · 3. Many to One Map With Entity Hierarchy ·...
Read more >
Many-to-one / one-to-many relations - typeorm - GitBook
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take...
Read more >
How to map a @ManyToOne association using a non-Primary ...
As you can see, the referencedColumnName allows you to customize the JOIN ON clause so that the isbn column is used instead of...
Read more >
RelationJoinColumnBuilder - typeorm
Builds join column for the many-to-one and one-to-one owner relations. Cases it should cover: when join column is set with custom name and...
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