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()
?
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.
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.
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) .
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With