JSON foreign keys in PostgreSQL

后端 未结 3 2021
星月不相逢
星月不相逢 2021-02-12 13:15

Is it possible to assign a foreign key to a json property in PostgreSQL? Here is an example what I would like to achieve, but it doesn\'t work:

CREATE TABLE User         


        
3条回答
  •  一个人的身影
    2021-02-12 13:43

    Here's a little SPI function have_ids which I use for an integrity constraint on a one-to-many relationship with a jsonb column

    CREATE TABLE foo (
      id INTEGER NOT NULL
    )
    
    CREATE TABLE bar (
      foo_ids pg_catalog.jsonb DEFAULT '[]'::jsonb NOT NULL,
      CONSTRAINT bar_fooids_chk CHECK (have_ids ('foo', foo_ids))
    )
    

    With a couple of triggers on foo it's almost as good as a foreign key.

提交回复
热议问题