简体   繁体   English

SQLAlchemy提交泡菜类型

[英]SQLAlchemy committing pickle types

I'm having an issue committing changes to pickle types (lists) in sqlalchemy. 我在提交对sqlalchemy中的泡菜类型(列表)进行更改时遇到问题。 It will act as if nothing happened after the committal. 承诺后好像什么也没发生。

Here's my my function where I try to commit: 这是我尝试提交的函数:

def commit_move(game_id, player, move):
    game = game_query(game_id)
    if player == 'human':
        game.human_spaces.append(move)
    if player == 'ai':
        game.ai_spaces.append(move)
    game.available_spaces.remove(move)
    print game.human_spaces
    print game.ai_spaces
    print game.available_spaces
    print "----"
    session.add(game)
    session.commit()

here's how the table is setup: 这是表格的设置方式:

class Game(Base):
    __tablename__ = 'game'
    id = Column(Integer, primary_key=True)
    human_spaces = Column(PickleType)
    ai_spaces = Column(PickleType)
    available_spaces = Column(PickleType)

here's the code I'm using to test it: 这是我用来测试的代码:

game_id = create_game()
print game_id
print get_available_spaces(game_id)
print get_human_spaces(game_id)
print get_ai_spaces(game_id)
print "---------"
commit_move(game_id, 'human', 7)
print get_available_spaces(game_id)
print get_human_spaces(game_id)
print get_ai_spaces(game_id)

and here's what the good ol' terminal is telling me: 这就是好的ol'终端告诉我的内容:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[]
[]
---------
[7]
[]
[1, 2, 3, 4, 5, 6, 8, 9]
----
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[]
[]

I'm sure it's something simple I'm missing here, but any help would be greatly appreciated! 我敢肯定,这里缺少一些简单的东西,但是任何帮助将不胜感激!

The problem is that the ORM is not alerted to changes inside a mutable type, like a list. 问题在于,不会向ORM发出有关诸如列表之类的可变类型内部更改的警报。 SQLAlchemy therefore offers mutation tracking with the sqlalchemy.ext.mutable extension. 因此, sqlalchemy.ext.mutable使用sqlalchemy.ext.mutable扩展名提供了突变跟踪。

From the examples in the documentation , in particular with reference to the sqlalchemy.ext.mutable.MutableList class, it looks like the column declaration should go (eg): 文档中的示例中,尤其是参考sqlalchemy.ext.mutable.MutableList类,看起来列声明应该走了(例如):

human_spaces = Column(MutableList.as_mutable(PickleType))

I quote from the documentation on the as_mutable method: "This establishes listeners that will detect ORM mappings against the given type, adding mutation event trackers to those mappings." 我从有关as_mutable方法的文档中引用:“这将建立侦听器,该侦听器将针对给定类型检测ORM映射,并向这些映射添加变异事件跟踪器。”

I've written a package to help make this easy. 我已经写了一个程序包来简化这个过程。 You can choose different encodings, including pickle , and easily dump and store objects to a database. 您可以选择不同的编码,包括pickle ,并轻松地将对象转储和存储到数据库。 It can connect to any database that sqlalchemy understands. 它可以连接到sqlalchemy理解的任何数据库。 There is a dictionary interface to a SQL table, and you can store any type that dill can serialize: SQL表有一个字典接口,您可以存储dill可以序列化的任何类型:

>>> import klepto
>>> db = klepto.archives.sqltable_archive('playgame')
>>> db['human'] = [1,2,3,4]
>>> db['ai'] = [1,2]
>>> db
sqltable_archive('sqlite:///:memory:?table=playgame', {'ai': [1, 2], 'human': [1, 2, 3, 4]}, cached=True)
>>> db.dump()
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM