Skip to content

Relation columns in embedded entities are not prefixed #6977

@nebkat

Description

@nebkat

Issue Description

When using relations inside embedded entities (@ManyToOne), their columns are not prefixed with the embedded entity prefix, unlike with normal @Columns. Whether a custom prefix is provided or the property name is used makes no difference.

See also: #3226, #3132 and #2254. Partially fixed by PR #3025.

Example scenario:

import { Column, createConnection, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class UserEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column(() => BanInfo)
    ban: BanInfo;
}

export class BanInfo {
    @Column({nullable: true}) reason: string;

    @ManyToOne(() => UserEntity, {nullable: true}) user: UserEntity;
}

createConnection().then(async () => {}).catch(error => console.log(error));

Expected Behavior

CREATE TABLE `user_entity` (
  `id` int NOT NULL AUTO_INCREMENT,
  `banUserId` int NULL,
  `banReason` varchar(255) NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB

Actual Behavior

CREATE TABLE `user_entity` (
  `id` int NOT NULL AUTO_INCREMENT,
  `userId` int NULL, # Missing ban prefix
  `banReason` varchar(255) NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB

Steps to Reproduce

  1. git clone https://github.com/nebkat/typeorm-issue-6977.git
  2. npm install
  3. npm run start

My Environment

Dependency Version
Operating System Arch Linux
Node.js version v14.14.0
Typescript version v4.0.3
TypeORM version v0.2.28

Additional Context

A single line change from #3025 (https://github.com/typeorm/typeorm/pull/3025/files#diff-c49455edefb07335bf9bd8b26a6fc6b15e6232177b5f56c2119d59043dd3a163R136) fixes the issue described above, however it is not compatible with relation ID columns. With this change applied, the code below produces a duplicate column error (both resolve correctly to banUserid but are not combined as would normally be expected).

export class BanInfo {
    @Column({nullable: true}) reason: string;

    @ManyToOne(() => UserEntity, {nullable: true}) user: UserEntity;
    @Column() userId: number;
}
CREATE TABLE `user_entity` (
  `id` int NOT NULL AUTO_INCREMENT,
  `banUserid` int NULL,
  `banReason` varchar(255) NULL,
  `banUserid` int NULL,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB

This change is definitely a step in the right direction, but I am not sure where to proceed to fix the conflicting columns issue.

Relevant Database Driver(s)

Not specific to any drivers.

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions