Skip to content

Commit

Permalink
fix(knex): always skip virtual properties in returning clause (#3699)
Browse files Browse the repository at this point in the history
Co-authored-by: Matheus Leonardo <mlsm@trialent.com>
  • Loading branch information
B4nan and mlsm-trl committed Nov 5, 2022
1 parent 9204cf5 commit c084dde
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Expand Up @@ -356,7 +356,7 @@ export abstract class AbstractSqlDriver<C extends AbstractSqlConnection = Abstra

if (this.platform.usesReturningStatement()) {
/* istanbul ignore next */
const returningProps = meta!.hydrateProps.filter(prop => prop.primary || prop.defaultRaw);
const returningProps = meta!.hydrateProps.filter(prop => prop.persist !== false && (prop.primary || prop.defaultRaw));
const returningFields = Utils.flatten(returningProps.map(prop => prop.fieldNames));
/* istanbul ignore next */
sql += returningFields.length > 0 ? ` returning ${returningFields.map(field => this.platform.quoteIdentifier(field)).join(', ')}` : '';
Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilderHelper.ts
Expand Up @@ -571,7 +571,7 @@ export class QueryBuilderHelper {
const useReturningStatement = type === QueryType.INSERT && this.platform.usesReturningStatement() && meta && !meta.compositePK;

if (useReturningStatement) {
const returningProps = meta!.hydrateProps.filter(prop => prop.primary || prop.defaultRaw);
const returningProps = meta!.hydrateProps.filter(prop => prop.persist !== false && (prop.primary || prop.defaultRaw));
qb.returning(Utils.flatten(returningProps.map(prop => prop.fieldNames)));
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/QueryBuilder.test.ts
Expand Up @@ -2480,7 +2480,7 @@ describe('QueryBuilder', () => {

const qb9 = pg.em.createQueryBuilder(Author2);
qb9.insert({ email: 'ignore@example.com', name: 'John Doe' }).onConflict('email').ignore();
expect(qb9.getQuery()).toEqual('insert into "author2" ("email", "name") values ($1, $2) on conflict ("email") do nothing returning "id", "hook_test", "created_at", "updated_at", "age", "terms_accepted"');
expect(qb9.getQuery()).toEqual('insert into "author2" ("email", "name") values ($1, $2) on conflict ("email") do nothing returning "id", "created_at", "updated_at", "age", "terms_accepted"');
expect(qb9.getParams()).toEqual(['ignore@example.com', 'John Doe']);

const timestamp = new Date();
Expand All @@ -2498,7 +2498,7 @@ describe('QueryBuilder', () => {
})
.where({ updatedAt: { $lt: timestamp } });

expect(qb10.getQuery()).toEqual('insert into "author2" ("created_at", "email", "name", "updated_at") values ($1, $2, $3, $4) on conflict ("email") do update set "name" = $5,"updated_at" = $6 where "updated_at" < $7 returning "id", "hook_test", "created_at", "updated_at", "age", "terms_accepted"');
expect(qb10.getQuery()).toEqual('insert into "author2" ("created_at", "email", "name", "updated_at") values ($1, $2, $3, $4) on conflict ("email") do update set "name" = $5,"updated_at" = $6 where "updated_at" < $7 returning "id", "created_at", "updated_at", "age", "terms_accepted"');
expect(qb10.getParams()).toEqual([timestamp, 'ignore@example.com', 'John Doe', timestamp, 'John Doe', timestamp, timestamp]);

const qb11 = pg.em.createQueryBuilder(Book2).where({ meta: { foo: 123 } });
Expand Down
3 changes: 3 additions & 0 deletions tests/issues/GH1626.test.ts
Expand Up @@ -34,6 +34,9 @@ export class Author {
return this.name ?? '~';
}

@Property({ persist: false })
foo?: string = '123';

}

describe('GH issue 1626', () => {
Expand Down

0 comments on commit c084dde

Please sign in to comment.