So, I have two Documents in MongoDB modelled in MongoEngine (classic relational database example)
class Person(Document)
name = StringField()
class Address(Document)
person = ReferenceField(Person)
city = StringField()
One person can have multiple addresses. I would like to migrate these models such that this becomes the new schema:
class Address(Document)
city = StringField()
class Person(Document)
name = StringField()
address = ListField(ReferenceField(Address))
This involves both setting and unsetting fields on the two schema's and on top of that making sure that the old Address entries are migrated into the correct Person address list.
In my mind it would go something like this:
PersonAddress add it to the correct Person by appending the ListFieldAddressThis seems like a trivial example with an easy enough solution although the implementation for mongoengine has eluded me for some time now.
By changing the classes in python the server crashes complaining that fields cannot be resolved. Also reordering the classes is an issue since class dependency on eachother switches. On top of that collections in MongoDB need to be updated.
I remember SQLAlchemy has good migration support but I haven't found anything similar to this for Flask + MongoEngine.
Anyone know a good solution for this?
Mongodb does not have relationship like sqlalchemy so what about something like this:
class Person(Document):
name = StringField()
address = ListField(DocumentField(Address))
city1 = Address(city='City1')
session.save(ad)
city2 = Address(city='City2')
session.save(city2)
person = Person(name='Ali', address=[city1, city2])
session.save(person)