Default value for JSONB types

Hello Forest Admin team,

I have a model with the following field:

methodology: {
    type: DataTypes.JSONB,
    defaultValue: Sequelize.JSONB('{"fr":""}'),
},

Expected behavior

I would expect the JSON field initialized with {"fr":""} when I create a new item (in the placeholder)

Actual behavior

The field is empty, no placeholder.

Context

Forest-express-sequelize: 6.3.11

Is there a way to make this work?

Thanks,
Gael

Hello @gaelperon!

Welcome to the Forest Admin Community!

I’ve just tried to reproduce your issue and ended up with form prefilled with the default value as you can see in this screenshot:

I’m using the last version of forest-express-sequelize.

Could you try with the last one, and let me know it everything is back to normal? :slight_smile:

Hello @Guillaume_Cisco

Thanks for the answer.

I updated “forest-express-sequelize” but the result is the same

After that, I also tried to upgrade “sequelize” and “pg” => I still get {} in the placeholder

When I try to update the model, everything is updated correctly.

Do you any other suggestions?

What’s wrong with updating to the last version of forest-express-sequelize?

I think it will solve your issue.

I have updated to version 6.7.6, but it doesn’t solve my problem. Sorry if it was unclear

Ok, so maybe this is something else.

Here is the psql dump I used for testing:

create table if not exists "user"
(
	methodology jsonb default '{"fr": ""}'::jsonb
);

Could you try creating this database with this table, create a Forest Admin project and let me know if it works?
Step by step, we will be able to find what is wrong with your current setup :slight_smile:

Ok, I’ve succeeded in reproducing your problem by setting:

defaultValue: Sequelize.JSONB('{"fr":""}'),

Could you replace it by:

defaultValue: '{"fr":""}',

And tell me if it works correctly?

This almost solved my problem. Thanks a lot

Now "{\"fr\":\"\"}" is displayed but the escaping characters are also displayed.

I don’t want these escaping characters. I just need {"fr":""}

I tried escaping with \ or \ with no success. Any idea ?

Hi @gaelperon :wave: I don’t know why the defaultValue is stringified two times but you can simply define your default value like this:

defaultValue: {"fr":""},

it fix the issue for me.
Let me know if it help.

Hi,

Another question on the same subject.

What should I use the get an empty array as default value
My field type is JSONB

I tried with :

defaultValue: '[]'
or
defaultValue: [],
or
defaultValue: Sequelize.literal('[]'),

The value is filled correctly in the form, but I have to remove the value and rewrite it to make it work.

Here is a screencast to understand the behavior :

Same problem with empty object : {}

What do you mean ? I have to create another topic ?

Sorry I misread the topic, I’m looking into it !

Do you get an error in your web console ?

Here are the logs :

vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24520 POST http://localhost:3310/forest/purchaseCategory 500 (Internal Server Error)
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24520
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:19477
send @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1762
ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1738
_ajaxRequest @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14905
_ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14908
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14905
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5772
e @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5772
ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14899
createRecord @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14876
createRecord @ client-9af9d637e32be14106bd9957ba3dcd2f.js:17971
n @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3459
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:15789
y @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5751
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5758
t.invoke @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5272
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5264
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5278
n._end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5341
n.end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5297
n._run @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5343
n._join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5342
n.join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5308
h @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3913
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3613
a @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3831
handleEvent @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3613
handleEvent @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3538
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3563
dispatch @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1548
g.handle @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1543
r @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24480
Show 3 more frames
vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:19462 n {isAdapterError: true, stack: "Error: Ember Data Request POST http://localhost:33…dor-755b2587ddb911f7820d11d5b5f8c8f7.js:24480:52)", description: undefined, fileName: undefined, lineNumber: undefined, …}description: undefinederrors: [{…}]fileName: undefinedisAdapterError: truelineNumber: undefinedmessage: "Ember Data Request POST http://localhost:3310/forest/purchaseCategory returned a 500↵Payload (application/json; charset=utf-8)↵[object Object]"name: "Error"number: undefinedstack: "Error: Ember Data Request POST http://localhost:3310/forest/purchaseCategory returned a 500↵Payload (application/json; charset=utf-8)↵[object Object]↵    at n (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14820:19)↵    at new n (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14824:54)↵    at n.handleResponse (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14894:26)↵    at n.handleResponse (http://app.forestadmin.com/assets/client-9af9d637e32be14106bd9957ba3dcd2f.js:17983:20)↵    at n [as handleResponse] (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3459:9)↵    at s (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14861:14)↵    at http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14904:8↵    at n.Ember.RSVP.Promise.d.error (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14904:19)↵    at u (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1466:118)↵    at Object.fireWith [as rejectWith] (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1467:698)↵    at k (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1750:468)↵    at XMLHttpRequest.<anonymous> (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1761:206)↵    at XMLHttpRequest.r (http://app.forestadmin.com/assets/vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24480:52)"__proto__: Error
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:19462
(anonymous) @ client-9af9d637e32be14106bd9957ba3dcd2f.js:14178
y @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5751
v @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5749
f @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5747
t.invoke @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5272
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5264
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5278
n._end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5341
n.end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5297
n._run @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5343
n._join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5342
n.join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5308
h @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3913
Ember.RSVP.Promise.d.error @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14905
u @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1466
fireWith @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1467
k @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1750
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1761
r @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24480
load (async)
c @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:19006
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24519
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24519
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:19477
send @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1762
ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1738
_ajaxRequest @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14905
_ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14908
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14905
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5772
e @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5772
ajax @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14899
createRecord @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:14876
createRecord @ client-9af9d637e32be14106bd9957ba3dcd2f.js:17971
n @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3459
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:15789
y @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5751
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5758
t.invoke @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5272
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5264
t.flush @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5278
n._end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5341
n.end @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5297
n._run @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5343
n._join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5342
n.join @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:5308
h @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3913
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3613
a @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3831
handleEvent @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3613
handleEvent @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3538
(anonymous) @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:3563
dispatch @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1548
g.handle @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:1543
r @ vendor-755b2587ddb911f7820d11d5b5f8c8f7.js:24480
Show 25 more frames

You have this error with all those syntaxes ?

defaultValue: '[]'
or
defaultValue: [],
or
defaultValue: Sequelize.literal('[]'),

Yes exactly, with all three syntaxes

Can you share the body of the request that returns this error please ?
And the body of a request returning a valid response ?

Body of the request returning an error

Ok and one that is valid ?