小编Hay*_*mas的帖子

SQLAlchemy 提交 pickle 类型

我在 sqlalchemy 中提交对 pickle 类型(列表)的更改时遇到问题。交付后它将表现得好像什么都没发生一样。

这是我尝试提交的功能:

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()
Run Code Online (Sandbox Code Playgroud)

该表的设置方式如下:

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

这是我用来测试它的代码:

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)
Run Code Online (Sandbox Code Playgroud)

这就是老终端告诉我的内容:

1
[1, 2, …
Run Code Online (Sandbox Code Playgroud)

python sql sqlalchemy commit pickle

3
推荐指数
1
解决办法
4474
查看次数

标签 统计

commit ×1

pickle ×1

python ×1

sql ×1

sqlalchemy ×1