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

前端 未结 4 1316
渐次进展
渐次进展 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:43

    One another approach would be to add column "photo_count" to users table, update it with triggers to make it reflect reality, and add check on it to enforce maximum number of photos.

    Side benefit from this is that at any given moment we know (without counting) how many photos user has.

    On other hand - the approach Quassnoi suggested is also pretty cool, as it gives you ability to reorder the photos in case user would want it.

提交回复
热议问题