【问题标题】:How to fix sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation)?如何修复 sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation)?
【发布时间】:2021-08-12 17:07:48
【问题描述】:

我尝试从我的烧瓶应用发布广告,但我不断从 Heroku 收到此错误,即使它在本地运行也是如此

'2021-05-24T11:17:34.712097+00:00 app[web.1]: sqlalchemy.exc.DataError: (psycopg2.errors.StringDataRightTruncation) value too long for type character varying(100)
2021-05-24T11:17:34.712098+00:00 app[web.1]: 
2021-05-24T11:17:34.712100+00:00 app[web.1]: [SQL: INSERT INTO ads (condition, phone, category, description, brand, negotiable, price, city, post_by_id, image, image_name, mimetype, post_on) VALUES (%(condition)s, %(phone)s, %(category)s, %(description)s, %(brand)s, %(negotiable)s, %(price)s, %(city)s, %(post_by_id)s, %(image)s, %(image_name)s, %(mimetype)s, now()) RETURNING ads.id]
2021-05-24T11:17:34.712170+00:00 app[web.1]: [parameters: {'condition': 'new', 'phone': '97', 'category': 'electronics', 'description': 'kjiji', 'brand': 'iphone', 'negotiable': 'yes', 'price': '6.00', 'city': 'Accra', 'post_by_id': 1, 'image': b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x05V\x00\x00\x03\x00\x08\x06\x00\x00\x00\xcf><\xc2\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x ... (1331901 characters truncated) ... 1l!+\x1d\xe9I6\xfa\x80*_n\xa7\x10\xf5c\x1e;\xf8:\xea\xb3c\xed\xd2+\xf3\xc6*\xa9\xbe\xbe\xde\xfe\x1f\x1e`\xa5\xb3\xc5S^Y\x00\x00\x00\x00IEND\xaeB`\x82', 'image_name': 'Screenshot_4.png', 'mimetype': 'image/png'}]
2021-05-24T11:17:34.712395+00:00 app[web.1]: (Background on this error at: http://sqlalche.me/e/9h9h)
2021-05-24T11:17:34.714853+00:00 app[web.1]: 10.102.190.208 - - [24/May/2021:11:17:34 +0000] "POST /post_add HTTP/1.1" 500 290 "https://buykev.herokuapp.com/post" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.62"
2021-05-24T11:17:34.715971+00:00 heroku[router]: at=info method=POST path="/post_add" host=buykev.herokuapp.com request_id=ce4101c4-e93b-41ac-b2a8-cb4a534ffc6a fwd="154.160.4.15" dyno=web.1 connect=1ms service=2459ms status=500 bytes=719 protocol=https
'

下面是我的数据库表

class Ads(db.Model):

    id = db.Column(db.Integer,primary_key =True)
    condition = db.Column(db.String(100))
    phone = db.Column(db.String(100))
    category = db.Column(db.String(100))
    description = db.Column(db.String(100))
    brand = db.Column(db.String(100))
    negotiable = db.Column(db.String(100))
    price = db.Column(db.String(100))
    city = db.Column(db.String(100))
    post_by_id = db.Column(db.Integer,db.ForeignKey('user.id'))
    image = db.Column(db.String(100))
    image_name = db.Column(db.String(100))
    mimetype =  db.Column(db.String(100))
    post_on = db.Column(DateTime(timezone=True), default=func.now())

【问题讨论】:

    标签: python postgresql flask heroku sqlalchemy


    【解决方案1】:

    image 列被声明为最大长度为 100 的 字符 数据,但您传递的 二进制 数据远多于 100 个字节:

    b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x05V\x00\x00\x03\x00\x08\x06\x00\x00\x00\xcf><\xc2\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x00\x ... (1331901 characters truncated) ... 1l!+\x1d\xe9I6\xfa\x80*_n\xa7\x10\xf5c\x1e;\xf8:\xea\xb3c\xed\xd2+\xf3\xc6*\xa9\xbe\xbe\xde\xfe\x1f\x1e\xa5\xb3\xc5S^Y\x00\x00\x00\x00IEND\xaeB\x82'
    

    如果你使用的是 Postgresql,你可以声明image而不指定长度:

    image = db.Column(db.String)
    

    但您可能会发现从数据库返回时处理数据有困难*。

    最好将image 声明为 BLOB(Binary Large OBject)类型:

    image = db.Column(LargeBinary)
    

    那么 SQLAlchemy 应该在检索数据时按预期返回字节。

    如果您更改现有数据库的列声明,您需要将更改应用到数据库本身,或者使用Flask Migrate 之类的工具,或者直接在 psql 控制台中:

     ALTER TABLE ads ALTER COLUMN image TYPE bytea USING image::bytea;
    

    * 这适用于 sqlite,但不能在 Postgresql 中正确往返 - 请参阅 this answer 了解有关此问题的更多讨论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-18
      • 2021-12-18
      • 2020-03-27
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 2019-12-18
      相关资源
      最近更新 更多