Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 是否将Storm实体复制到另一个存储?_Python_Storm Orm - Fatal编程技术网

Python 是否将Storm实体复制到另一个存储?

Python 是否将Storm实体复制到另一个存储?,python,storm-orm,Python,Storm Orm,我有一个模块,它使用Storm ORM将数据保存在本地SQLite数据库中。我正在开发另一个模块,该模块将数据同步到中心PostgreSQL服务器。我想我会很聪明,做以下几件事: unsynced = localStore.find(MyType, MyType.synced == False) for assign in unsynced: self.remoteStore.add(assign) 这并不像预期的那样有效,引发了以下错误: object at 0x18bb4d0 i

我有一个模块,它使用Storm ORM将数据保存在本地SQLite数据库中。我正在开发另一个模块,该模块将数据同步到中心PostgreSQL服务器。我想我会很聪明,做以下几件事:

unsynced = localStore.find(MyType, MyType.synced == False)

for assign in unsynced:
    self.remoteStore.add(assign)
这并不像预期的那样有效,引发了以下错误:

object at 0x18bb4d0 is part of another store

是否有办法打破与本地存储的关联,以便远程保存数据?这可能会稍微复杂一些,因为在成功远程保存数据后,我需要在本地副本中翻转Synched标志。

Storm中没有自动执行此操作的功能,因为应该复制什么并不总是显而易见的:

  • 表示表的模型类可能无法完全描述基础表。可能还有其他字段没有从Python端使用
  • 不清楚如何处理外键/引用。您是否也复制了相关对象?您是否保持外键ID不变,并希望在插入第二个数据库时不会导致完整性错误?您是否尝试在另一个数据库中找到要引用的等效对象?您将使用哪个键进行搜索
  • 当然,这些问题可能不会影响您的应用程序。在这种情况下,您可以编写一个方法来复制对象,以便将其添加到另一个存储:

    def copy(self):
        """Create a new object with the same property values."""
        other = self.__class__()
        # Copy over the relevant fields.  If you're using an integer primary key,
        # then this probably doesn't include that key.
        other.foo = self.foo
        other.bar = self.bar
        ...
        return other
    
    然后,您可以修改代码以执行以下操作:

    for assign in unsynced:
        self.remoteStore.add(assign.copy())