I made a API system with flask-restplus
and mongoengine
Also I'm new at mongodb and mongoengine.
I know that there is no foreign key in mongodb.
But after read mongoengine's docs, I found some similar fields.
ReferenceField is made fields that has relationship like RDB.
So I wonder that, If I set some field to ReferenceField
, it works like RDB's foreign key?
Look at the below code about db schema.
class User(Document):
no = SequenceField()
userid = StringField(unique=True, required=True)
userpw = StringField(required=True)
class Article(Document):
no = SequenceField()
subject = StringField(required=True)
content = StringField(required=True)
userid = ReferenceField(User, required=True, dbref=True)
When userid john
is deleted in User
schema, john's every article have to deleted.
Also every Article
's userid have to already contain in User
schema.
I know it is to easy if I use RDB foreign key and cascade option.
But I wonder that, if I use mongoengine's ReferenceField
, it can be possible?
Or, What is the exact work ReferenceField
do?
Thanks.