Kononnable / typeorm-model-generator

Generates models for TypeORM from existing database.
MIT License
1.51k stars 281 forks source link

Add option to create an ID field #65

Closed miltonhowe closed 6 years ago

miltonhowe commented 6 years ago

When the entity has a foreign key, the generator creates a property for the foreign table as an entity, and then leaves out the id field which keyed to the foreign entity. There should be an option to include both the foreign entity and the key from the original table. It would be nice as well if it could intelligently look for the Id suffix and remove and include it correctly.

For example:

@ManyToOne(type => User, consumerUserId => consumerUserId.listings, {nullable: false, onDelete: "NO ACTION", onUpdate: "NO ACTION"})
@JoinColumn({name: "consumer_user_id"})
consumerUser: User;

@Column("char", {
    name: "consumer_user_id",
    nullable: false,
    length: 255,
})
consumerUserId: string;

The Id gets chopped off for the Entity and included for the Id (here a string, sometimes an INT or other)

Kononnable commented 6 years ago

As for the removing of Id sufix: I was thinking for some time of more generic solution. I'm planing to implement something similar to typeorm custom naming strategy. Then removing Id sufix from relation names could be default behavior, but users would have the ability to change it if it doesn't suits theirs case.

Kononnable commented 6 years ago

Implemented in typeorm-model-generator@0.2.17

maxpain commented 5 years ago

@Kononnable why this generator by default removes Id suffix?

Kononnable commented 5 years ago

Most of the time columns with this sufix are used only to relate two entities together. If you see a property(in typescript class) named categoryId you expect it type to be number, not Category. Default naming strategy remove this sufix because of such cases. Just take a look at sample2-one-to-one in /test/integration/examples/ if it's not clear.

maxpain commented 5 years ago

In our cases we often using user_id columns in selects. Using join to users table just for retreive id fields is stupid. Imho.

Kononnable commented 5 years ago

I'm think you're looking for @RelationId decorator.

As for joining tables just to get Id - most(not sure if every) of modern db drivers should generate query plan which won't touch related table.