简体   繁体   English

Sqlalchemy 问题并将 jsonb 数组插入到 postgresql

[英]Issue with Sqlalchemy and inserting array of jsonb to postgresql

So i'm trying to insert an array of jsonb values into my database but I can't seem to format it right, here's my code:所以我试图将一组 jsonb 值插入到我的数据库中,但我似乎无法正确格式化,这是我的代码:

updated_old_passwords.append({"index": 1, "password": hashed_password})
user.old_passwords = updated_old_passwords
user.last_password_reset = datetime.datetime.utcnow()
db.session.commit()

And here's the error:这是错误:

ProgrammingError: (psycopg2.ProgrammingError) column "old_passwords" is of type jsonb[] but expression is of type text[]
LINE 1: ...-01-05T06:18:24.992968'::timestamp, old_passwords=ARRAY['"\"...
                                                             ^
HINT:  You will need to rewrite or cast the expression.
 [SQL: 'UPDATE users SET password=%(password)s, last_password_reset=%(last_password_reset)s, old_passwords=%(old_passwords)s WHERE users.id = %(users_id)s'] [parameters: {'users_id': 1, 'password': '$6$rounds=656000$b.LVoVb7T0WNbT.n$l9uUb1a1qk2Z5ugfpI7B.3D02sUVqhES5VhM1TvwUnMd/iZZL3gn4/zExB47/ZQYPcTMRxO1iaL4/yjXda2.P1', 'last_password_reset': datetime.datetime(2017, 1, 5, 6, 18, 24, 992968), 'old_passwords': ['"\\"{\\\\\\"index\\\\\\": 1, \\\\\\"password\\\\\\": hashed_password}\\""']}]

Any idea how I format my insert for this to work?知道我如何格式化我的插入以使其正常工作吗?

Here's my db table这是我的数据库表

from sqlalchemy.dialects.postgresql import JSONB, ARRAY

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(255), index = True)
    password = db.Column(db.String(255))
    last_password_reset = db.Column(db.DateTime())
    old_passwords = db.Column(ARRAY(JSONB))

I also tried this:我也试过这个:

updated_old_passwords.append(cast('{"index": 1, "password": hashed_password}', JSONB))

but got the error但得到了错误

StatementError: (exceptions.TypeError) <sqlalchemy.sql.elements.Cast object at 0x10f3ed150> is not JSON serializable [SQL: u'UPDATE users SET password=%(password)s, last_password_reset=%(last_password_reset)s, old_passwords=%(old_passwords)s WHERE users.id = %(users_id)s'] [parameters: [{'users_id': 1, 'password': '$6$rounds=656000$WYOiWMAYDSag9QIX$YSDtZle6Bd7Kz.cy7ejWq1NqgME.xUPiDHfV31FKobGu2umxoX34.ZP2MrUDxyym0X4fyzZNEIO//yS6UTPoC.', 'last_password_reset': datetime.datetime(2017, 1, 5, 6, 26, 35, 610703), 'old_passwords': [<sqlalchemy.sql.elements.Cast object at 0x10f3ed150>]}]]

Add the missing cast, using:添加缺少的演员表,使用:

class CastingArray(ARRAY):

 def bind_expression(self, bindvalue):
     return cast(bindvalue, self)

And when defining the module use this class instead of ARRAY ( old_passwords = db.Column(CastingArray(JSONB))并且在定义模块时使用这个类而不是ARRAY ( old_passwords = db.Column(CastingArray(JSONB))

(Answer taken from https://groups.google.com/forum/#!topic/sqlalchemy/oB4zVgUEMgA ) (答案取自https://groups.google.com/forum/#!topic/sqlalchemy/oB4zVgUEMgA

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

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