Does CouchDB supports referential integrity?

前端 未结 3 2102
一个人的身影
一个人的身影 2021-02-07 17:56

I am new to CouchDB and learning about it. I did not come across CouchDB support for referential integrity. Can we create a foreign key for a field in the CouchDB document?

3条回答
  •  囚心锁ツ
    2021-02-07 18:29

    No, CouchDB doesn't do foreign keys as such, so you can't have it handle the referential integrity of the system for you. You would need to handle checking for vendors in the application level.

    As to whether you can make a field a primary key, the primary key is the _id field, but you can use any valid json as a key for the views on the db. So, for instance you could create a view of orders with their vendor as the key.

    something like

    function(doc) {
      if (doc.type == 'order')
        emit(doc.vendor,doc);
    }
    

    would grab all the docs in the database that have a type attribute with the value order and add them to a view using their vendor as the key.

    Intro to CouchDB views

提交回复
热议问题