How to write a constraint concerning a max number of rows in postgresql?

前端 未结 4 1310
渐次进展
渐次进展 2020-12-01 13:57

I think this is a pretty common problem.

I\'ve got a table user(id INT ...) and a table photo(id BIGINT, owner INT). owner is a reference o

4条回答
  •  攒了一身酷
    2020-12-01 14:56

    You cannot write such a constraint in a table declaration.

    There are some workarounds:

    • Create a trigger that would check the number of photos for each user
    • Create a photo_order column that would keep the order of photos, make (user_id, photo_order) UNIQUE, and add CHECK(photo_order BETWEEN 1 AND 10)

提交回复
热议问题