1

I have an entity and service defined as follows.

  1. user.entity.js
@Entity({ name: 'users' })
export class User {
  @Column({
    type: 'binary',
    length: 16,
    select: false,
    primary: true,
    default: () => `(UUID_TO_BIN(UUID(), 1))`,
  })

  id: string;
  @Index()
  @Column({ type: 'varchar', length: 225, nullable: false })
  name: string;

  @Column({ type: 'varchar', length: 15, unique: true, nullable: false })
  MSISDN: string;

  @Column({ type: 'varchar', length: 225, unique: true, nullable: false })
  email_address: string;

  @Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
  created_at: Date;

  @Column({ type: 'datetime', nullable: true, onUpdate: 'CURRENT_TIMESTAMP' })
  updated_at: Date;
}
  1. user.service.js
@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private usersRepository: Repository<User>,
  ) {}

  create(user: DeepPartial<User>): Promise<User> {
    return this.usersRepository
      .createQueryBuilder()
      .insert()
      .values({ ...user})
      .execute();
  }
}

Whenever I attempt to create a user from the Postman client, the record is in fact inserted into the database but I get a 500 response from the postman client.

Postman response

{
    "statusCode": 500,
    "message": "Internal server error"
}

The CLI error looks like this:

[Nest] 9084   - 15/03/2021, 11:17:47   [ExceptionsHandler] Cannot update entity because entity id is not set in the entity. +331142ms
Error: Cannot update entity because entity id is not set in the entity.
    at C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\query-builder\ReturningResultsEntityUpdator.js:128:39
    at Array.map (<anonymous>)
    at ReturningResultsEntityUpdator.<anonymous> (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\query-builder\ReturningResultsEntityUpdator.js:122:46)
    at step (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\node_modules\tslib\tslib.js:141:27)
    at Object.next (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\node_modules\tslib\tslib.js:122:57)
    at C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\node_modules\tslib\tslib.js:115:75
    at new Promise (<anonymous>)
    at Object.__awaiter (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\node_modules\tslib\tslib.js:111:16)
    at ReturningResultsEntityUpdator.insert (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\query-builder\ReturningResultsEntityUpdator.js:87:24)
    at InsertQueryBuilder.<anonymous> (C:\Users\Mnengwa\workspace\kodisha\kodisha-web-api\node_modules\typeorm\query-builder\InsertQueryBuilder.js:105:76)
2
  • 1
    I get the same error, did you find a solution to this issue ? Commented May 10, 2021 at 19:42
  • Any updates? I have the same issue. Commented Sep 2, 2021 at 13:59

2 Answers 2

3

I had to specify the reload option as false in my save call (not insert) to make the error go away.

For insert you want to add an updateEntity(false) call to your chain before calling execute.

There's more context here: https://github.com/typeorm/typeorm/issues/4651

Sign up to request clarification or add additional context in comments.

Comments

0

Try defining the id attribute for the User entity as follows -

@PrimaryColumn()
@Generated('uuid')
id: string;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.