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.

Does not resolve correct column name with @JoinColumn

See original GitHub issue

Issue Description

if you have a column let’s say “user_id” while using @Column({ name: “user_id” })

and you have other table like photos and you want to reference user_id you get “Error: Referenced column user_id was not found in entity User”

I’m pretty sure it tries to find the column by property name. It should also try finding it by declared value from @Column decorator

Expected Behavior

If it does not fiend property it should look from decorator values…

Actual Behavior

It says:

Error: Referenced column user_id was not found in entity User" even though the column exists

Steps to Reproduce

Create user table like:

@Column({ name: "user_id" })
public userId;

create another table trying to reference it

 @Column({ name: "user_id", type: "uuid", nullable: false })
 public userId: string;

 @JoinColumn({
      { name: "user_id", referencedColumnName: "user_id" },
  })
public user: User

My Environment

Dependency Version
Operating System win10
Node.js version v14.15.0
Typescript version v4.0.5
TypeORM version v0.2.31

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • maraidb
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don’t know how to start. I would need guidance.
  • No, I don’t have the time, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
JamesParkDevcommented, Feb 18, 2021

If you want to create relation you should use relation decorators. You should read docs but basically what you need to do if you want, for instance, one-to-many relation

class User{
@Column({ name: "user_id" })
  public userId;
}

class Photo{
  @Column({ name: "user_id", type: "uuid", nullable: false })
   public userId: string;

  @ManyToOne((type) => User)
  @JoinColumn({
      { name: "user_id", referencedColumnName: "user_id" },
  })
  public user: User
}

Thank you for your reply. This does not function at all for me I found a solution that works.

used “userId” in my referencedColumnName

  @ManyToOne((type) => User)
  @JoinColumn({
      { name: "user_id", referencedColumnName: "userId" },
  })
  public user: User

and in other tables I have

  @Column({ name: "user_id" })
  public userId;

only doing userId works for me. Even though it should also work with column name

2reactions
msabo1commented, Feb 16, 2021

If you want to create relation you should use relation decorators. You should read docs but basically what you need to do if you want, for instance, one-to-many relation

class User{
@Column({ name: "user_id" })
  public userId;
}

class Photo{
  @Column({ name: "user_id", type: "uuid", nullable: false })
   public userId: string;

  @ManyToOne((type) => User)
  @JoinColumn({
      { name: "user_id", referencedColumnName: "user_id" },
  })
  public user: User
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA Cannot resolve column/IntelliJ - java - Stack Overflow
Open the Persistence tool window (View | Tool Windows | Persistence). Right-click the necessary module, persistence unit or session factory, and select Assign ......
Read more >
PM80441: DEFAULT JOIN-COLUMN IS NOT BEING ... - IBM
When the SQL for a named query is generated, the "name" attribute of a @JoinColumn is ignored when the "table" attribute.
Read more >
How to change the @OneToOne shared primary key column ...
In this article, we are going to see how you can change the @OneToOne shared primary key column name when using JPA and...
Read more >
@JoinColumn Annotation Explained | Baeldung
The annotation javax.persistence.JoinColumn marks a column as a join column for an entity association or an element collection. In this quick ...
Read more >
Chapter 2. Mapping Entities - Red Hat on GitHub
Note that the logical column name is not necessarily equals to the property name esp when the ... Try hard to fix your...
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