SQLAlchemy提交泡菜类型 [英] SQLAlchemy committing pickle types

查看:90
本文介绍了SQLAlchemy提交泡菜类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交对sqlalchemy中的泡菜类型(列表)所做的更改时遇到问题.承诺后好像什么也没发生.

I'm having an issue committing changes to pickle types (lists) in 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()

这是表格的设置方式:

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)

这是好的ol'终端告诉我的内容:

and here's what the good ol' terminal is telling me:

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!

推荐答案

问题是ORM不会在可变类型(如列表)中发出警报.因此,SQLAlchemy提供了带有sqlalchemy.ext.mutable扩展名的突变跟踪.

The problem is that the ORM is not alerted to changes inside a mutable type, like a list. SQLAlchemy therefore offers mutation tracking with the sqlalchemy.ext.mutable extension.

摘录自文档,尤其是对于sqlalchemy.ext.mutable.MutableList类的引用,看起来列声明应该去了(例如):

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 (e.g.):

human_spaces = Column(MutableList.as_mutable(PickleType))

我引用了as_mutable方法的文档: 这将建立侦听器,该侦听器将检测针对给定类型的ORM映射,并向这些映射添加变异事件跟踪器."

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."

这篇关于SQLAlchemy提交泡菜类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆