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
<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
<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
<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();