JSON in columns (postgresql)
Hey <a:YA_Wave:743393941369651201>
I am completely new to using SQL databases and have a (hopefully) simple question. I want to store JSON inside a column and want to make certain keys not null and/or set a default value, how would I do that?
In this case, I have a
language
column and want to set a default value for the locale
key
This is my UserTable type:
This is the current state of my migrations/0-initials.ts
(stripped things away that are not needed for this post)
Thanks 6 Replies
Ah okey thank you!
And when I get the data out of the db, I would have to JSON.parse() all jsob columns? Is there a way to directly get it as json?
pg
driver does usually parse json for you, but there are some weird corner cases where it doesn't. But usually there's no need to parse. You might need to stringify the inputs in some insert and update queries. If I recall correctly, postgres stringifies objects but not arrays.
You can use ColumnType
to allow strings in inputs, but not in output: https://kyse.link/?p=s&i=hRzxktOvhghrlyUMfpWs
The reason you need to stringify in defaultTo
is because there are no parameters in DDL queries. Everything's always inlinethank you alot
Will play around with it in a few hours and come back if I have issues!
It says that type
binary
does not exist..?
my code:
data-type-node.d.ts
: I think binary is a MySQL data type
Postgres has at least
bytea