Skip to content

Commit

Permalink
fix: null as default value (#845)
Browse files Browse the repository at this point in the history
even though modeling is weird as the db default is anyway null, our
implementation should go along with it.
 
closes #773
  • Loading branch information
johannes-vogel authored Oct 15, 2024
1 parent 10e0534 commit 0041ec0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ class HANAService extends SQLService {
let extract = sql ?? `${this.quote(name)} ${this.insertType4(element)} PATH '$.${name}'`
if (!isUpdate) {
const d = element.default
if (d && (d.val !== undefined || d.ref?.[0] === '$now')) {
const defaultValue = d.val ?? (cds.context?.timestamp || new Date()).toISOString()
if (d && ('val' in d || d.ref?.[0] === '$now')) {
const defaultValue = 'val' in d ? d.val : (cds.context?.timestamp || new Date()).toISOString()
managed = typeof defaultValue === 'string' ? this.string(defaultValue) : defaultValue
}
}
Expand Down
2 changes: 1 addition & 1 deletion postgres/lib/PostgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ GROUP BY k
}

defaultValue(defaultValue = this.context.timestamp.toISOString()) {
return this.string(`${defaultValue}`)
return typeof defaultValue === 'string' ? this.string(`${defaultValue}`) : defaultValue
}

static Functions = { ...super.Functions, ...require('./cql-functions') }
Expand Down
14 changes: 13 additions & 1 deletion test/compliance/INSERT.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require('../cds')
const cds = require('../cds')

describe('INSERT', () => {
const { data, expect } = cds.test(__dirname + '/resources')
data.autoIsolation(true)

describe('into', () => {
test.skip('missing', () => {
throw new Error('not supported')
Expand Down Expand Up @@ -32,4 +35,13 @@ describe('INSERT', () => {
throw new Error('not supported')
})
})

describe('default values', () => {
test('default values are generated', async () => {
const { 'basic.literals.defaults': entity } = cds.db.entities
await cds.run(INSERT.into(entity).entries({ID: 1}))
const result = await cds.run(SELECT.from(entity, 1))
expect(result).to.deep.eq({ ID: 1, boolean: false, integer: 0, nulls: null, string: ''})
})
})
})
10 changes: 9 additions & 1 deletion test/compliance/resources/db/basic/literals.cds
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ entity binaries {
largebinary : LargeBinary;
}

entity defaults {
key ID : Integer;
string : String default '';
boolean : Boolean default false;
integer : Integer default 0;
nulls : String default null;
}


/* Excluded from the tests until fully supported
entity vectors {
vector : Vector;
}
*/
*/

0 comments on commit 0041ec0

Please sign in to comment.