App Engine标准上的Postgres - 插入时出错

时间:2017-03-01 10:46:10

标签: python postgresql google-app-engine

App引擎标准只支持python 2,库必须是纯python,所以我使用SQLAlchemy和pg8000(纯python而不是psycopg2,而不是psycopg2)连接到Postgres实例。连接没有任何问题,但是当尝试插入时,我得到以下错误,我不完全理解:

<type 'memoryview'> has type <type 'memoryview'>, but expected one of: str, unicode

导致此行为的代码段:

event = self.generate_view(body)
self.write_event(event, ViewEvent)

def write_event(self, event, event_model):
    session = Session()
    success = False
    try:
        new_event = event_model(**event)
        session.add(new_event)
        session.commit()
        logging.info('Generated event {}'.format(event))
        success = True
    except Exception, e:
        session.rollback()
        logging.debug('Error inserting event {}'.format(event))
        logging.debug('Error was {}'.format(e))
    finally:
        session.close()
    return success

非常感谢任何帮助 - 或者如果您已成功将Postgres连接到App Engine Standard,我可以使用任何替代解决方案,而不仅仅是使用pg8000。

1 个答案:

答案 0 :(得分:0)

您可能需要将ssl=True添加到数据库连接。

pg8000.connect(ssl=True, **config)

或者在SQLAlchemy中:

create_engine('postgresql+pg8000://user:pass@hostname/dbname', connect_args={'ssl': True}, echo=True)