Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set AUTO_INCREMENT for @PrimaryColumn()?

How can I set AUTO_INCREMENT for @PrimaryColumn()?

I know that @PrimaryGeneratedColumn() does this but I want to have ID from the double type.

Is it possible to have @PrimaryGeneratedColumn() with double type? if not have can I set AUTO_INCREMENT for @PrimaryColumn()?

like image 889
Mohammad Avatar asked Feb 25 '20 19:02

Mohammad


People also ask

How can I get auto increment ID?

To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with “id” as auto-increment. Inserting records into the table. To display all the records.

How do I add an auto increment to an existing column?

Here's the syntax of ALTER TABLE statement, ALTER TABLE table_name MODIFY column_name INT NOT NULL AUTO_INCREMENT PRIMARY KEY; In the above statement, you need to specify the table_name and column_name. Here's the SQL statement to add AUTO INCREMENT constraint to id column.

How can create auto increment in SQL Server?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .

How can I change column auto increment in SQL Server?

Go to Identity Specifications and explore it. Make (Is Identity) row as Yes and by default Identity Increment row and Identity Seed row become 1. In case we want to automatically increase the value of this column by 2 (like 1, 3, 5, 7 etc.) then change the value of Identity Seed to 2.


1 Answers

Please use this code.

import {Entity, PrimaryGeneratedColumn} from 'typeorm';

@Entity()
export class SomeWhat{

    @PrimaryGeneratedColumn('increment')
    public id: number;
....

For double ID, we can't use AUTO_INCREMENT. AUTO_INCREMENT is just available for int type.

  • @PrimaryGeneratedColumn

    We can set AUTO_INCREMENT at only @PrimaryGeneratedColumn decorator. The below code is the declaration of the @PrimaryGeneratedColumn. As you can see, we can use 2 types of strategy (increment, uuid).

    (typeorm/decorator/columns/PrimaryGeneratedColumn.d.ts)

    export declare function PrimaryGeneratedColumn(): Function;
    export declare function PrimaryGeneratedColumn(options: PrimaryGeneratedColumnNumericOptions): Function;
    export declare function PrimaryGeneratedColumn(strategy: "increment", options?: PrimaryGeneratedColumnNumericOptions): Function;
    export declare function PrimaryGeneratedColumn(strategy: "uuid", options?: PrimaryGeneratedColumnUUIDOptions): Function;
    
  • @PrimaryColumn

    But in this @PrimaryColumn, there is no available strategy. This is just transforming the value to the primary key without generating.

    (typeorm/decorator/columns/PrimaryColumn.d.ts)

    export declare function PrimaryColumn(options?: ColumnOptions): Function;
    export declare function PrimaryColumn(type?: ColumnType, options?: ColumnOptions): Function;
    
like image 179
tpikachu Avatar answered Sep 29 '22 04:09

tpikachu