sqlalchemy.exc.DataError:(psycopg2.DataError)值太长,类型字符不同(20)

时间:2017-04-19 02:59:43

标签: python heroku sqlalchemy

尝试在heroku上注册用户,但是我在heroku日志中收到此错误,并且我收到内部错误,并且我引用了this

该网站的其余部分可以使用,我只能在he​​roku上注册,在本地主机上工作。

这是烧瓶,有什么建议吗?

>>> from app import db,models
>>> u = models.User(username='susan', password='janemba133')
>>> db.session.add(u)
>>> db.session.commit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 157, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 801, in commit
    self.transaction.commit()
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 392, in commit
    self._prepare_impl()
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
    self.session.flush()
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2019, in flush
    self._flush(objects)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2137, in _flush
    transaction.rollback(_capture_exception=True)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2101, in _flush
    flush_context.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
    rec.execute(self)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 532, in execute
    uow
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 174, in save_obj
    mapper, table, insert)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 800, in _emit_insert_statements
    execute(statement, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 914, in execute
    return meth(self, multiparams, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
    context)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception
    exc_info
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 200, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
    context)
  File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.DataError) value too long for type character varying(20)
 [SQL: 'INSERT INTO "user" (username, password) VALUES (%(username)s, %(password)s) RETURNING "user".id'] [parameters: {'username': 'susan', 'password': '$2b$09$Vez1laSR6yrYxARBQGzF.Oxl/rJVtxN2.uuaXzJK6zberdikWy98K'}]

Models.py

from app import app, db, bcrypt, slugify, JWT, jwt_required, current_identity, safe_str_cmp
from sqlalchemy import Column, Integer, DateTime, func
from app import (TimedJSONWebSignatureSerializer
                          as Serializer, BadSignature, SignatureExpired)

import datetime


class User(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(20), unique=True)
    # posts = db.relationship('Post', backref='author', lazy='dynamic')

    def __init__(self, username, password):
        self.username = username
        self.password = bcrypt.generate_password_hash(password, 9)


    def is_authenticated(self):
        return True


    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return  (self.id)

    def __repr__(self):
        return '<User %r>' % self.username



class Post(db.Model):
    __tablename__ = "posts"

    id = db.Column(db.Integer,  primary_key=True)
    title = db.Column(db.String(80))
    body = db.Column(db.Text)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    slug = db.Column(db.String(80), index=True, nullable=True)
    author = db.relationship("User",backref=db.backref("posts",lazy="dynamic"))

    time_created = Column(DateTime(timezone=True), server_default=func.now())
    time_updated = Column(DateTime(timezone=True), onupdate=func.now())

    def __init__(self, title, body, slug,author):
        self.title = title
        self.body = body
        self.slug = slugify(title).lower()
        self.author = author

requirements.txt

Flask==0.10.1
Flask-SQLAlchemy==2.1
gunicorn==19.4.5
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
psycopg2==2.7.1
SQLAlchemy==1.0.12
Werkzeug==0.11.9
simplejson==3.10.0
six==1.10.0
slugify==0.0.1
Flask-Bcrypt==0.7.1
Flask-HTTPAuth==3.2.2
Flask-Jsonpify==1.5.0
Flask-JWT==0.3.2
Flask-Login==0.4.0
Flask-Migrate==2.0.3
Flask-Script==2.0.5
Flask-WhooshAlchemy==0.56
Flask-Session==0.3.1
PyJWT==1.4.2
bcrypt==3.1.3

1 个答案:

答案 0 :(得分:1)

我修复了它,我必须做的是将我的密码max增加到120而不是20,因为它使用了bcrypt,所以它可以占用很多字符。

销毁数据库, 并做一个

db.create_all()

你应该好好去。