question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

defaultTo() doesn't JSON.stringify json columns

See original GitHub issue

I’m on PostgreSQL 9.3.4 and ran

knex.schema.table('user', function(t) {
  t.json('preferences').defaultTo({}).notNullable();
}).then(function () {
  console.log('modified columns');
});

and got the result

             Column             |         Type          |                Modifiers
--------------------------------+-----------------------+------------------------------------------
 preferences                    | text                  | not null default '[object Object]'::text

It seems that defaultTo() should call JSON.stringify().

P.S. I would think that table column type would be json, but I didn’t set up this server, so it’s possible that the json extensions are not enabled.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
JetFaultcommented, Mar 17, 2015

Doing t.json('data').defaultTo('{}'); works and checking PG returns as a default of '{}'::json so all good.

1reaction
tgriessercommented, May 20, 2014

Could you just do .defaultTo('{}')?

Read more comments on GitHub >

github_iconTop Results From Across the Web

defaultTo() doesn't JSON.stringify json columns #280 - GitHub
I'm on PostgreSQL 9.3.4 and ran knex.schema.table('user', function(t) { t.json('preferences').defaultTo({}).notNullable(); }).then(function ...
Read more >
Data from the object doesn't convert when using JSON.stringify
I'm stuck in this weird issue that I'm having hard time in understanding what's doing on. When a button is clicked it calls...
Read more >
39 JSON in Oracle Database
Insert JSON data into the JSON column, using any of the methods available for Oracle Database. The following statement uses a SQL INSERT...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >
Schema Builder | Knex.js
Adds a json column, using the built-in json type in PostgreSQL, MySQL and SQLite, defaulting to a text column in older versions or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found