rqlite, nextjs, typeorm pain of `id:null` for an insert to a table

Issue:

  • i got an AUTOINCREMENT, number field ‘id’ in my table (DB: rqlite)
  • when i insert a partial row (no mandatory fields except primary key) i don’t get the id value back from typeorm which was inserted in the DB
  • following in my setup and the multiple code i tried (check the service below)

entity definition

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class Requests {
@PrimaryGeneratedColumn('increment')
id: number;
@Column({ nullable: true })
name: string;
...}
</code>
<code>export class Requests { @PrimaryGeneratedColumn('increment') id: number; @Column({ nullable: true }) name: string; ...} </code>
export class Requests {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ nullable: true })
  name: string;

...}

migration table

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TABLE "requests" (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name varchar,
...);
</code>
<code>CREATE TABLE "requests" ( id INTEGER PRIMARY KEY AUTOINCREMENT, name varchar, ...); </code>
CREATE TABLE "requests" (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name varchar,
...);

in the service (issue here)

unable to get the auto-incremented value back despite create/insert/upsert/save… i need to know the id (primary key) of the row that was inserted

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
public async saveRequest(passedPartialRequest: Partial<Requests> | any) {
// case 1: if we don't provide an id, we get "TypeORMError: Cannot update entity because entity id is not set in the entity."
// if we provide id:null or id:0, we get id: null or id:0 (even though the correct incremented id is created in the DB)
const case1 = await this.requestsRepository.save({
...passedPartialRequest,
id: null,
});
// case 2: inserted a row but id came back as undefined (from the DB)
const case2 = await this.requestsRepository.create({
passedPartialRequest,
});
// case 3: need to provide id, else we get "TypeORMError: Cannot update entity because entity id is not set in the entity."
const case3 = await this.requestsRepository.insert({
...passedPartialRequest,
id: null,
});
// case 4: TypeORMError: Cannot update entity because entity id is not set in the entity,
// if we provide null, we get identifier: null
const saved4 = await this.requestsRepository
.createQueryBuilder()
.insert()
.values({
...passedPartialRequest,
id: null,
})
.execute();
// case 5: this also returns identifier: null - we need to provide id:null to prevent the TypeORMError above
const case5 = await this.requestsRepository.upsert(
{ ...passedPartialRequest, id: null },
['id']
);
// case 6: OUTPUT or RETURNING clause only supported by Microsoft SQL Server or PostgreSQL or MariaDB databases.
const case6 = await this.requestsRepository
.createQueryBuilder()
.insert()
.into('requests')
.values([passedPartialRequest])
.returning('INSERTED.*')
.printSql()
.execute();
</code>
<code> public async saveRequest(passedPartialRequest: Partial<Requests> | any) { // case 1: if we don't provide an id, we get "TypeORMError: Cannot update entity because entity id is not set in the entity." // if we provide id:null or id:0, we get id: null or id:0 (even though the correct incremented id is created in the DB) const case1 = await this.requestsRepository.save({ ...passedPartialRequest, id: null, }); // case 2: inserted a row but id came back as undefined (from the DB) const case2 = await this.requestsRepository.create({ passedPartialRequest, }); // case 3: need to provide id, else we get "TypeORMError: Cannot update entity because entity id is not set in the entity." const case3 = await this.requestsRepository.insert({ ...passedPartialRequest, id: null, }); // case 4: TypeORMError: Cannot update entity because entity id is not set in the entity, // if we provide null, we get identifier: null const saved4 = await this.requestsRepository .createQueryBuilder() .insert() .values({ ...passedPartialRequest, id: null, }) .execute(); // case 5: this also returns identifier: null - we need to provide id:null to prevent the TypeORMError above const case5 = await this.requestsRepository.upsert( { ...passedPartialRequest, id: null }, ['id'] ); // case 6: OUTPUT or RETURNING clause only supported by Microsoft SQL Server or PostgreSQL or MariaDB databases. const case6 = await this.requestsRepository .createQueryBuilder() .insert() .into('requests') .values([passedPartialRequest]) .returning('INSERTED.*') .printSql() .execute(); </code>

  public async saveRequest(passedPartialRequest: Partial<Requests> | any) {
    // case 1: if we don't provide an id, we get "TypeORMError: Cannot update entity because entity id is not set in the entity."
    // if we provide id:null or id:0, we get id: null or id:0 (even though the correct incremented id is created in the DB)
    const case1 = await this.requestsRepository.save({
      ...passedPartialRequest,
      id: null,
    });

    // case 2: inserted a row but id came back as undefined (from the DB)
    const case2 = await this.requestsRepository.create({
      passedPartialRequest,
    });

    // case 3: need to provide id, else we get "TypeORMError: Cannot update entity because entity id is not set in the entity."
    const case3 = await this.requestsRepository.insert({
      ...passedPartialRequest,
      id: null,
    });

    // case 4: TypeORMError: Cannot update entity because entity id is not set in the entity,
    // if we provide null, we get identifier: null
    const saved4 = await this.requestsRepository
      .createQueryBuilder()
      .insert()
      .values({
        ...passedPartialRequest,
        id: null,
      })
      .execute();

    // case 5: this also returns identifier: null - we need to provide id:null to prevent the TypeORMError above
    const case5 = await this.requestsRepository.upsert(
      { ...passedPartialRequest, id: null },
      ['id']
    );

    // case 6: OUTPUT or RETURNING clause only supported by Microsoft SQL Server or PostgreSQL or MariaDB databases.
    const case6 = await this.requestsRepository
      .createQueryBuilder()
      .insert()
      .into('requests')
      .values([passedPartialRequest])
      .returning('INSERTED.*')
      .printSql()
      .execute();    

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật