Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 金字塔事务管理器在更新时未提交_Python_Postgresql_Sqlalchemy_Pyramid - Fatal编程技术网

Python 金字塔事务管理器在更新时未提交

Python 金字塔事务管理器在更新时未提交,python,postgresql,sqlalchemy,pyramid,Python,Postgresql,Sqlalchemy,Pyramid,对不起,我之前不清楚 编辑: 我使用默认的金字塔应用程序和sqlalchemy(后端:postgresql),使用 pcreate -s alchemy 所以我有所有与DBSession、pyramid_tm等相关的设置。我有这个代码 class User(Base): id = Column(Integer, primary_key=True) connection = relationship("UserConnection", uselist=False, backref

对不起,我之前不清楚

编辑: 我使用默认的金字塔应用程序和sqlalchemy(后端:postgresql),使用

pcreate -s alchemy
所以我有所有与DBSession、pyramid_tm等相关的设置。我有这个代码

class User(Base):
    id = Column(Integer, primary_key=True)
    connection = relationship("UserConnection", uselist=False, backref="user")

class UserConnection(Base):
    userid = Column(Integer, ForeignKey('user.id'), primary_key=True)
    friends = Column(JSON)

def add_connection(user, friend):
    with transaction.manager:
        if not user.connection:
            user_conn = UserConnection(userid=user.id, friends=[])
        else:
            user_conn = user.connection

        user_conn.friends.append(friend)
        print(user_connection.friends)
        session.add(user_conn)
当我第一次运行add_connection()时,它是user.connection。创建了新记录,但在下一次运行时(若转到else),记录不会得到更新,在控制台上,我只能看到ROLLBACK/COMMIT,但没有其他语句


那里的print语句显示了更新的结果,但数据库没有更新。

您应该在请求范围内使用事务

sqlalchemy和pyramid_tm可以帮你做到这一点。 您可以使用我的代码:

pyramid_sqlalchemy.py
使用pip和调用配置安装zope.sqlalchemy和pyramid\u tm。include(pyramid\u sqlalchemy)

了解到我不需要每次都使用transaction.manager,反正这是由pyramid\u tm完成的。
# -*- coding: utf-8 -*-

""" Pyramid sqlalchemy lib.

Session will be available as dbsession attribute on request.

! Do not close session on your own.

"""

import sqlalchemy
from sqlalchemy.orm import sessionmaker, scoped_session
from zope.sqlalchemy import ZopeTransactionExtension

Session = scoped_session(sessionmaker(
    extension=ZopeTransactionExtension()
))


def includeme(config):
    """ Setup sqlalchemy session connection for pyramid app.

    :param config: Pyramid configuration

    """
    config.include('pyramid_tm')
    # creates database engine from ini settings passed to pyramid wsgi
    engine = sqlalchemy.engine_from_config(
        config.get_settings(), connect_args={
            'charset': 'utf8'
        }
    )
    # scoped session gives us thread safe session
    Session.configure(bind=engine)
    # make database session available in every request
    config.add_request_method(
        callable=lambda request: Session, name='dbsession', property=True
    )