mrzhangboss / json2db

convert json to relational db
http://json2db.zhanglun.work
MIT License
26 stars 15 forks source link

sqlalchemy.exc.InvalidRequestError raise #32

Open mrzhangboss opened 5 years ago

mrzhangboss commented 5 years ago

I meet a interest bug. When debug is fine, it run error.I guess it maybe garbage problem.Because I add force gc.collect(), this bug disappear.I try use a simple test to reproduce this issue.I guess sqlalchemy save some meta info to raise this problem.

DBU = 'sqlite:///test.sqlite3'

class TestJSON2DB(TestCase):

    def doOtherThing(self):
        factory = CommonModelFactory(default_db_url=DBU, use_foreign_key=False)

        d1 = {'father': "aa", "son1": [{"aa": "b", "sonss": [{"jack": "jj"}]}]}
        m1 = factory.from_json(data=d1, root_name='aa')

        # m1.create_tables_in_db()
        gc.collect()
        # m1.delete_tables_in_db()
        # self.mmm = weakref.ref(m1._db_models['aa'])
        assert m1.db_models
        self.mmm = m1._db_models['aa']

        for k in list(m1._db_models.keys()):
            del m1._db_models[k]
        gc.collect()

    def testFatherChange(self):
        # gc.collect()

        self.doOtherThing()
        factory = CommonModelFactory(default_db_url=DBU, use_foreign_key=False)

        d3 = {'new': "aa"}
        m3 = factory.from_json(data=d3, root_name='bbq')
        m3.create_tables_in_db()

        m3.store(data=d3)

        m3.delete_tables_in_db()
mrzhangboss commented 5 years ago

If you try delete self.mmm and collect the garbage, it's ok

    def testFatherChange(self):
        # gc.collect()

        self.doOtherThing()
        del self.mmm
        gc.collect()

        factory = CommonModelFactory(default_db_url=DBU, use_foreign_key=False)

        d3 = {'new': "aa"}
        m3 = factory.from_json(data=d3, root_name='bbq')
        m3.create_tables_in_db()

        m3.store(data=d3)

        m3.delete_tables_in_db()