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.

Creating Custom Primary Key Generated Column Value

See original GitHub issue

Looking for a way to create some custom Primary Key value generated for Column in entity and that should be auto incremented.

Ex: @Entity(“DemandRecord”) export class DemandRecord {

@CustomColumn()
 demandRecordNumber: number;

Ex:- demandRecordNumber values should be like YYYYMMDD001,YYYYMMDD002..like this where YYYYMMDD is year,month and date.

Am i not able to find any approach for this. In Hibernate we have ways to this but in TypeORM..i am not able to find any documents regarding this

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

42reactions
pleerockcommented, Apr 25, 2018

There is no need for additional functionality in there.

Alternative to that approach you can do:

@Entity()
export class Post {

   @PrimaryColumn()
   id: string;

  @BeforeInsert()
  private beforeInsert() {
         this.id = generateAnything();
  }

}
1reaction
webleafcommented, Apr 23, 2019

@pleerock for example in SQLite (primary column in SQLite is autogenerated too) if I try to do something like this (saving multiply objects at once)

@Entity()
class SomeClass {

    @PrimaryColumn()
    public id: number;

    @AfterInsert()
    private async afterInsert() {
        const result = await getRepository(SomeClass).query("SELECT last_insert_rowid() last_id");
        this.id = result[0]["last_id"]
    }

    constructor(id?: number) {
        if (typeof id === "number") {
            this.id = id
        }
    }
}

getRepository(SomeClass).save([
    new SomeClass(-3),
    new SomeClass(-2),
    new SomeClass(), // expect -1, but afterInsert will get 2
    new SomeClass(), // expect 0, but afterInsert will get 2
    new SomeClass(), // expect 1, but afterInsert will get 2
    new SomeClass(), // expect 2, but afterInsert will get 2
]);

Such log will be produced:

JS: query: BEGIN TRANSACTION
JS: query: INSERT INTO "some_class"("id") VALUES (?) -- PARAMETERS: [-3]
JS: query: INSERT INTO "some_class"("id") VALUES (?) -- PARAMETERS: [-2]
JS: query: INSERT INTO "some_class"("id") VALUES (NULL) -- PARAMETERS: []
JS: query: INSERT INTO "some_class"("id") VALUES (NULL) -- PARAMETERS: []
JS: query: INSERT INTO "some_class"("id") VALUES (NULL) -- PARAMETERS: []
JS: query: INSERT INTO "some_class"("id") VALUES (NULL) -- PARAMETERS: []
JS: query: SELECT last_insert_rowid() last_id
JS: query: SELECT last_insert_rowid() last_id
JS: query: SELECT last_insert_rowid() last_id
JS: query: SELECT last_insert_rowid() last_id
JS: query: SELECT last_insert_rowid() last_id
JS: query: COMMIT

And all objects with undefined id will get id 2, because all @AfterInsert executes after all insert queries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - How to create a custom auto generated ID number for a ...
If you want to make Product_No the primary key - just use this SQL syntax: CREATE TABLE Product ( ID INTEGER IDENTITY(1,1) NOT...
Read more >
How to Automatically Generate Primary Key Values Using ...
This database uses the auto-increment field, so Vertabelo provides a straightforward way of creating a primary key. First, go to Table ...
Read more >
Defining an Auto Increment Primary Key in SQL Server - Chartio
The first is PRIMARY KEY , which as the name suggests, forces the specified column to behave as a completely unique index for...
Read more >
Generated Values - EF Core - Microsoft Learn
Database columns can have their values generated in various ways: primary key columns are frequently auto-incrementing integers, ...
Read more >
Auto-generated primary keys: UUID, serial or identity column?
But typically, there is no such attribute, and you have to generate an artificial primary key. Some people even argue that you should...
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