文本数组中的Postgres检查约束,以验证值的有效性

时间:2019-07-19 11:31:31

标签: sql postgresql

我想创建与此类似的东西

CHECK (ALL(scopes) IN ('read', 'write', 'delete', 'update'))

scopes这是表中的字段text[],我想确保此数组中的所有值都是以上值之一。有什么意见吗?还有可能通过SELECT从另一个表中获取这些值吗?

我已经看到了以下解决方案,但我想知道是否有一个更简单的解决方案。

Postgresql check constraint on all individual elements in array using function

1 个答案:

答案 0 :(得分:4)

demo:db<>fiddle

使用<@ operator

for fle in all_files:
   # open the file and then call .read() to get the text
   with open(os.path.join(path, fle),"rb") as f:
      text = f.read()
      new_list.append(text)

不知道您的确切用例,但我希望使用更规范的解决方案:将这四个操作放在一个单独的表中,该表可以更新。然后,您可以使用外键而不是检查约束。如果必须更新这四个关键字,则无需更改表DDL,而只需更改外部表中的值即可。