【问题标题】:Issue with Sqlalchemy and inserting array of jsonb to postgresqlSqlalchemy 问题并将 jsonb 数组插入到 postgresql
【发布时间】:2017-01-05 07:42:24
【问题描述】:

所以我试图将一组 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()

这是错误:

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}\\""']}]

知道如何格式化我的插入以使其正常工作吗?

这是我的数据库表

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))

我也试过这个:

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

但出现错误

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>]}]]

【问题讨论】:

  • stackoverflow.com/questions/41132714/… 是 mysql 但它仍然有效
  • @e4c5 是的,我可以正常化,我几乎需要存储用户使用的最后 10 个密码,并且在重置时不允许他们重新使用。所以添加新密码时需要弹出第10个使用过的密码。
  • 是的,这是使用 JSONB 的合理情况。我已经将它与 django 一起使用,但没有与 sqlalchemy 一起使用,因此无法为您提供特定的语法。但是,因为它听起来不像您将在该字段上进行任何查找。为什么不只是数组?
  • @e4c5 我不确定 Array 是否总是相同的顺序,因为我必须弹出最后一个。在那种情况下,我可以

标签: python postgresql sqlalchemy jsonb


【解决方案1】:

添加缺少的演员,使用:

class CastingArray(ARRAY):

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

并且在定义模块时使用这个类而不是ARRAY (old_passwords = db.Column(CastingArray(JSONB))

(答案来自https://groups.google.com/forum/#!topic/sqlalchemy/oB4zVgUEMgA)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2020-06-28
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多