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.

Diversions on SQL generated for CreateDateColumn on query Builder and Entity.create()

See original GitHub issue

Issue Description

Hello, I’m having some issues with the generated SQL on Entity.create(), I’m using postgresql and typescript.

Right now I have a simple entity:

import { Field, ObjectType } from 'type-graphql';
import {
  BaseEntity,
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@ObjectType()
@Entity()
export class Post extends BaseEntity {
  @Field()
  @PrimaryGeneratedColumn()
  id!: number;

  @Field(() => String)
  @CreateDateColumn()
  createdAt = Date;

  @Field(() => String)
  @UpdateDateColumn()
  updatedAt = Date;

  @Field()
  @Column()
  title!: string;
}

But when I try to create an item like this:

Post.create({ title: "This is a title" }).save();

The SQL returns with an error:

query: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), $1) RETURNING "id", "createdAt", "updatedAt" -- PARAMETERS: ["Teste de titulo 3"]
query failed: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), $1) RETURNING "id", "createdAt", "updatedAt" -- PARAMETERS: ["Teste de titulo 3"]
error: error: syntax error at or near "Aug"

One way to solve the issue was to use a query builder like this:

await getConnection()
      .createQueryBuilder()
      .insert()
      .into(Post)
      .values({
        title: "This is a title",
      })
      .returning('*')
      .execute();

with the returning sql being:

query: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (DEFAULT, DEFAULT, $1) RETURNING * -- PARAMETERS: ["Teste de titulo 3"]

Which works.

Am I doing something wrong?

Expected Behavior

The use of Entity.create should return a SQL statement like this:

query: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (DEFAULT, DEFAULT, $1) RETURNING * -- PARAMETERS: ["Teste de titulo 3"]

Actual Behavior

Right now the SQL generated by Entity.crete is this:

query: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), $1) RETURNING "id", "createdAt", "updatedAt" -- PARAMETERS: ["Teste de titulo 3"]
query failed: INSERT INTO "post"("createdAt", "updatedAt", "title") VALUES (Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), Sat Aug 14 2021 15:09:17 GMT-0300 (Brasilia Standard Time), $1) RETURNING "id", "createdAt", "updatedAt" -- PARAMETERS: ["Teste de titulo 3"]
error: error: syntax error at or near "Aug"

Steps to Reproduce

You can follow the issue description

My Environment

Dependency Version
Operating System Ubuntu WSL
Node.js version v16.4.2
Typescript version 4.3.5
TypeORM version 0.2.37

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
imnotjamescommented, Aug 14, 2021

For questions, please check out the community slack or check TypeORM’s documentation page on other support avenues - cheers!

1reaction
imnotjamescommented, Aug 14, 2021

Actually, I see your issue.

created = Date

Passing a function as a value will cause the result to be inserted directly without escaping for an insert.

You’re assigning the function Date which returns a Date object. That’s converted to a string.

Instead use

created: Date
Read more comments on GitHub >

github_iconTop Results From Across the Web

@CreateDateColumn and @UpdateDateColumn should use ...
Use repository.update() to override column values with ... I'm trying to generate some fixtures in MongoDB and it seems to not be working ......
Read more >
Node.js add created_at and updated_at in entity of typeorm
Let's import first import { CreateDateColumn,UpdateDateColumn } from "typeorm";. Add this fields and decarators in Entity
Read more >
Select using Query Builder - typeorm - GitBook
getOne(). It builds the following SQL query: SELECT. user.id as userId, ... There are several ways how you can create a Query Builder...
Read more >
Create a Query Using the Query Builder - Salesforce Help
Use the Query Builder to create the query without having to manually create the query in SQL. All you have to do is...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
When you work with QueryBuilder , think like you are creating an SQL query. In this example, "photo" and "metadata" are aliases applied...
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