如何在Postgres中执行JSON/JSONB的形状?
我试图在JSON中存储对调查问题的回答,因为它们可能在布尔值(这是栏?是/否),数字(foo?),字符串(描述什么是foo)。它运行良好,但是我该如何执行某个问题,JSON的形状相同?
例如,对于“您每天要吃多少个Foo或酒吧?”的问题, 我期望以下结构(假设它是列答案
):
{
"foo": "number",
"bar": "number"
}
我该如何执行并保持数据一致?
I am trying to store the response to questions of a survey in JSON, as they could be in boolean(Is this Bar? Yes/No), number(How much is Foo?), string(Describe what is Foo). It is working fine, but how can I enforce that for a certain question, the JSON will be of identical shape?
For example, for the question "How many Foo or Bar do you eat everyday?",
I am expecting the following structure(let's say it is column answer
):
{
"foo": "number",
"bar": "number"
}
How can I enforce that and keep my data consistent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样做的“正确”方法是使您的数据正常化或在应用程序级别执行模式。
Postgres JSON是毫无用处的。这个想法是,如果您的数据结构化,则可能应在普通列中。但是,在数据结构但架构是动态的情况下,JSON是一个完全有效的选择。在这种情况下,最好在应用程序层执行模式约束。
There 'right' way yo do this is either to normalize your data or to enforce schema at the application level.
Postgres JSON is delibrately schemaless. The idea is that if your data is structured it should probably be in normal columns. However, JSON is a totally valid option in the situation where the data is structured but the schema is dynamic. In those situations it's best to enforce schema constraints at the application layer.
通常,这个话题是广泛而复杂的。对于那些有兴趣的人,我建议 json Schema 网站。
出于我们的目的,我们可以使用一种非常简单的验证方法,仅限于两个问题,在功能体系中发表了评论:
在 db<> fiddle。
如您所见,该模式只是一个格式良好的答案的一个示例。为每个问题定义一个模式,为答案表创建一个触发器,然后在触发函数中使用上述函数来验证新的或修改的答案。
In general, the topic is extensive and complex. For those interested, I recommend the JSON Schema website.
For our purposes, we can use a very simple method of validation, limited to two issues, commented in the function body:
Test the function in Db<>Fiddle.
As you can see, the pattern is just an example of a well-formatted answer. Define a pattern for each question, create a trigger for the answers table and use the above function inside the trigger function to verify new or modified answers.