Sequelize-typescript: UUID as primary key

Created on 21 Sep 2017  路  10Comments  路  Source: RobinBuschmann/sequelize-typescript

Hi, I have a table like this
export default class MyTable extends Model<MyTable> { @PrimaryKey @Column(DataType.UUID) id:string; @Column name:string; }

is there any way to insert one record and it manages to create the uuid directly? for example if I do
queryInterface.bulkInsert("MyTable", [ { name: "test" }]);
it says "null value in column "id" violates not-null constraint"

I guess I would be able to manage it with hooks but I would like to know if exists a more elegant solution

Thanks

Most helpful comment

Anyway, to people looking for the same issue (using postgres), at the end I had to install uuid-ossp and after that this worked:

@PrimaryKey
@Column({type:DataType.UUID,defaultValue:Sequelize.fn("uuid_generate_v4")})
id:string;

All 10 comments

Hey, @Default(DataType.UUIDV4) or @Column({defaultValue: DataType.UUIDV4}) (or whatever UUID version you need) should do the trick.

Robin sorry, I didnt know about default.
Thanks

No problem :)

Anyway, to people looking for the same issue (using postgres), at the end I had to install uuid-ossp and after that this worked:

@PrimaryKey
@Column({type:DataType.UUID,defaultValue:Sequelize.fn("uuid_generate_v4")})
id:string;

@oscarcalvo Thanks! I really have same problem锛宐ut anyway @RobinBuschmann I think it is sequelize's bug which had fix here https://github.com/sequelize/sequelize/issues/1490. But it seems still not work in sequelize-typescript.

@ckt1010 So the following is not working with sequelize-typescript:

@PrimaryKey
@Default(DataType.UUIDV4)
@Column(DataType.UUID)
id: string;

?

@RobinBuschmann yes. here is sql:
Executing (default): CREATE TABLE IF NOT EXISTS "_User" ("id" UUID ,...
didn't see any default value setting.

@RobinBuschmann Oh, BTW, my dialect is pgsql, is this database not support very well on sequelize-typescript? Since when I use @UpdatedAt and hope update 'updatedAt' column automatically, but not work also.

@ckt1010 @Default(DataType.UUIDV4) @Column(DataType.UUID) works as intended. See http://docs.sequelizejs.com/manual/tutorial/models-definition.html#data-types. It says

defaultValue: Sequelize.UUIDV1 or Sequelize.UUIDV4 to make sequelize generate the ids automatically)

Which means that sequelize creates the UUID programmatically (This is common practice).

Regarding your @UpdatedAt issue: Can you provide an example or better an example repo which reproduces the issue (For me it is working very well)? In general sequelize-typescript supports all dialects as good as sequelize does.

@RobinBuschmann Thanks a lot! It's my mistake, I thought Sequelize will use sql like @Default(false). And @UpdatedAt also have the same misunderstanding.
Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josecolella picture josecolella  路  4Comments

fareshan picture fareshan  路  3Comments

lilling picture lilling  路  4Comments

libvirtadept picture libvirtadept  路  4Comments

fareshan picture fareshan  路  4Comments