Python sqlalchemy.exc.DataError:(psycopg2.DataError)值太长,无法更改类型字符

Python sqlalchemy.exc.DataError:(psycopg2.DataError)值太长,无法更改类型字符,python,postgresql,sqlalchemy,psycopg2,Python,Postgresql,Sqlalchemy,Psycopg2,我尝试使用SQL Alchemy在postgresql数据库中保存哈希密码。 表脚本为: Create Table "User"( Id serial Primary key, UserName varchar(50) unique not null, Nikname varchar(50) not null, "password" varchar(172) not null, FirstName varchar(75) not null, LastNam

我尝试使用SQL Alchemy在postgresql数据库中保存哈希密码。 表脚本为:

Create Table "User"(
Id serial Primary key,
UserName varchar(50) unique not null,
Nikname varchar(50) not null,
"password" varchar(172) not null,
FirstName varchar(75) not null,
LastName varchar(75) not null,
BirthDate date not null,
CreateDate date not null,
Status smallint Not null
)
这是地图:

user = Table('User', metadata,
         Column('id', Sequence(name='User_id_seq'), primary_key=True),
         Column('username', String(50), unique=True, nullable=False),
         Column('nikname', String(50), nullable=False),
         Column('firstname', String(75), nullable=False),
         Column('lastname', String(75), nullable=False),
         Column('password', String(172), nullable=False),
         Column('status', Integer, nullable=False),
         Column('birthdate', Date, nullable=False),
         Column('createdate', Date, nullable=False)
    )
当我尝试插入数据时,出现以下异常:

sqlalchemy.exc.DataError: (psycopg2.DataError) value too long for type character varying(172)
[SQL: 'INSERT INTO "User" (id, username, nikname, firstname, lastname, password, status, birthdate, createdate) VALUES (nextval(\'"User_id_seq"\'), %(username)s, %(nikname)s, %(firstname)s, %(lastname)s, %(password)s, %(status)s, %(birthdate)s, %(createdate)s) RETURNING "User".id'] [parameters: {'username': 'hoseinyeganloo@gmail.com', 'nikname': 'Laughing Death', 'firstname': 'Hosein', 'lastname': 'Yegnloo', 'password': b'i1SlFeDkCZ0BJYanhINGCZC80rqVYABHAS/Ot2AWDgzPZCtshMNRZGHeosx3PvLqsCWzZfPZpsT+UZZLShmQxfbO5VJ4xJbLNjbb0n8HuazQy+0u5Ws2DCtmdDh+HFBTKCAiNuzUGueurP9d2VE3tHwHpX+hCMS1RB4KIOUORKw=', 'status': 1, 'birthdate': datetime.datetime(1990, 3, 1, 0, 0), 'createdate': datetime.datetime(2017, 6, 23, 0, 0)}]
但正如你们所看到的,数据是完全适合太领域,并没有错误,当我在pgadmin内执行这个查询! 我想问题出在我的地图上。我已将字符串更改为文本,但出现错误:|

有什么想法吗

我不知道这是否有用。当所有字符都是数字时,代码工作没有错误

我尝试插入一些数字,而不是散列密码,它的工作

更新
我发现问题是字符编码!SQLAlchemy如何增加传递字符串的大小!现在我正在努力阻止它

问题不在于映射、字符集或任何类似sql炼金术的东西! 这是我的密码!当我尝试将哈希结果转换为base64字符串时,结果将是BinaryString!不是一串

“密码”:b'i1SlFeDkCZ0BJYanhING

所以为了解决这个问题,我需要在将base64结果保存到数据库之前将其解码为unicode字符串

u、 密码=u.password.decode(“utf-8”,“忽略”)


这个问题的产生是因为postgresql和其他ORM数据结构定义是严格类型的,不像SQL那样松散。我从两个方面确定了问题

  • db.列定义
  • 按方法过帐数据应遵循模型方法args order
  • 其中,由于上述内容可能不会影响SQLAlchemy db交互,迁移到其他ORM需要您遵守上述内容

  • 表定义

    __tablename__ = 'test'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    accountNumber = db.Column(db.String(255))
    description = db.Column(db.String(255), nullable=False)
    accountType = db.Column(db.String(255), nullable=False)
    
  • 按列角色顺序过帐数据

    createAccount(accountNumber, description, accountType)
    

  • 如果需要对哈希结果进行base64,则可能是做错了什么。1.您可以在数据库中存储二进制数据,而无需base64的存储开销——使用。2.密码散列的流行实现为您提供了
    crypt
    格式的散列(看起来像
    $2a$blahblahblah
    ),它首先是一个简单的ASCII字符串。你应该用它来散列你的密码。非常感谢。bytea不是我需要的,但crypt格式和passlib正是我想要的:)