skip to Main Content

I am inserting date in my database with a defined createAt date. I don’t want to use now() as default in postgres. So I have create my column as such using typeorm.

@Field(() => DateTimeScalar)
@Column("timestamp without time zone",{})
createdAt: Date;

and when I am inserting in my database I am creating a date as such:

Date("2022-05-08 00:49:23").toIsoString()

and then create an object as such Interaction.create({createdAt: createdDate}), I later add this interaction to my array of interactions and save them:

await getConnection().getRepository(Interaction).save(interactions)

The createdAt value is always overwritten with the current date, I even checked postgres and I can confirm there is no now() as default for this column.

Any ideas how do I disable this autogen option?

2

Answers


  1. You can use @CreateDateColumn decorator. link

    @CreateDateColumn()
    createdAt: Date;
    

    Type ORM will do all the stuff for you. You don’t need to set it, just leave this field alone.

    const entity = new Entity();
    entity.name = 'EntityName';
    // entity.createdAt = new Date(); // don't do it
    manager.save(entity);
    
    Login or Signup to reply.
  2. As I wrote in comment, I had a very similar issue. Reported in on Github (https://github.com/typeorm/typeorm/issues/9966), but eventually solved it myself.

    The reason for the problem I had was that my build command was not emptying out dist folder and thus I had older files sitting in it, which Typeorm (by my design) was automatically loading and using.

    So check if you have or had any subscribers, which are automatically loading and thus rewriting your data in the middle of calls.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search