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

Can't Order Columns on Inherited Entities #7687

Open
1 of 20 tasks
timmclaurin opened this issue May 27, 2021 · 3 comments
Open
1 of 20 tasks

Can't Order Columns on Inherited Entities #7687

timmclaurin opened this issue May 27, 2021 · 3 comments

Comments

@timmclaurin
Copy link

timmclaurin commented May 27, 2021

Issue Description

When a base classs is used, e.g. audit fields, they some times come up first with the tables are generated, but sometimes they come up last:
image

These two tables extend from the same base entity.

Expected Behavior

Seems to be a reference here: #541

Why is this a bug request not a feature request? Because TypeORM is awesome in general, yet not following/allowing for best practices in database design really lets it down. When we have multiple levels of Inheritance, things get even worse. The table columns order makes less sense, and when using a base class that has a blob type, now all of a sudden i can have a blob in the middle of my table, vs the end.

Actual Behavior

Inherited columns can show up at the end, beginning, or in the case of multiple levels, middle of the table.

Steps to Reproduce

My Environment

Node v15.14.0
├── typeorm@0.2.32
└── typescript@4.2.4

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • [ x] 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.
@timmclaurin
Copy link
Author

Bump to see if there is any thoughts around this.

@FacundoVazquez
Copy link

FacundoVazquez commented Jan 13, 2022

+1

EDIT: I found this: https://stackoverflow.com/questions/54932600/typeorm-entity-inheritance-manytoone-relationship.

You can do something like this:

class MyEntity {
  // base columns

  @Column(() => Persistable, { prefix: false })
  persistable: Persistable;
}
export abstract class Persistable {
  @Column({ type: 'varchar', length: 255, nullable: false })
  createdBy: string;

  @Column({ type: 'varchar', length: 255, nullable: false })
  updatedBy: string;

  @CreateDateColumn({ type: 'timestamp', precision: 0, default: () => 'CURRENT_TIMESTAMP' })
  createdAt: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    precision: 0,
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updatedAt: Date;
}

@kodmanyagha
Copy link

kodmanyagha commented Jul 17, 2023

Year 2023, Jul. Probably this problem stil didn't solved. persistable method is working but this isn't solve main side of the problem. We really need to set order number for each column. We must able to set id column as first column, deletedAt last (-1), updatedAt -2, createdAt -3 etc. Negative numbers must start from last, positive first. Why nobody solves this problem? Eloquent and doctrine didn't solve too. I didn't understand why.

Edit: For now I'm using that method: create two classes (ExampleEntity, InsertableExampleEntity), First class is extending other (ExampleEntity extends InsertableExampleEntity). Id and timestamps are living in ExampleEntity, business logic fields are living in InsertableExampleEntity. Now if you generate migration then id and timestamp fields will be live last in migration's SQL commands. I'm changing this SQL, moving id column at the top of the table and executing migrations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants