Migrations in mongoengine: InvalidId

前端 未结 2 602
南旧
南旧 2021-02-06 09:43

I am working with mongoengine and trying to do a simple migration. I have a field which I would like to migrate from being a StringField to a ReferenceField to another Object.

2条回答
  •  伪装坚强ぢ
    2021-02-06 10:29

    I've always migrated things by hand using an intermediate collection or an intermediate field in the same collection, but this example makes it look like you don't have to. Stack overflow hates external links so I'm including the example verbatim below. BTW be careful with that "drop_collection" part!

    import unittest
    from mongoengine import *
    
    
    class Test(unittest.TestCase):
    
        def setUp(self):
            conn = connect(db='mongoenginetest')
    
        def create_old_data(self):
            class Person(Document):
                name = StringField()
                age = FloatField()  # Save as string
    
                meta = {'allow_inheritance': True}
    
            Person.drop_collection()
    
            Person(name="Rozza", age=35.00).save()
    
            rozza = Person._get_collection().find_one()
            self.assertEqual(35.00, rozza['age'])
    
        def test_migration(self):
    
            self.create_old_data()
    
            class Person(Document):
                name = StringField()
                age = StringField()
    
                meta = {'allow_inheritance': True}
    
            for p in Person.objects:
                p.age = "%s" % p.age
                p.save()
    
            rozza = Person.objects.first()
            self.assertEqual("35.0", rozza.age)
    
    if __name__ == '__main__':
        unittest.main()
    

提交回复
热议问题