首页 > 解决方案 > sqlalchemy :必须使用非空名称构造列

问题描述

我尝试将新记录插入到 amazon redshift 上的表中。基本上我从谷歌电子表格下载行并尝试作为数据框推送到表格中。

from sqlalchemy import create_engine
engine = create_engine('postgresql://redshift_user:password,GB8@redshift-url:5439/datawarehouse')
connection = engine.raw_connection()

然后我执行

df.to_sql(name='test',con=engine, if_exists='append', index=False)

但它不起作用,它引发了异常

sqlalchemy.exc.ArgumentError: Column must be constructed with a non-blank name or assign a non-blank .name before adding to a Table.

我检查了一些帖子并尝试了他们的解决方案但仍然无法正常工作

标签: pythonpostgresqlsqlalchemyamazon-redshift

解决方案


尝试这个:

import SQLAlchemy sqla

db_uri = ''
db = sqla.create_engine('postgresql://file.db')


class Table(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)

    def __repr__(self):
        return f'USER[{self.id}, {self.name]'
db.create_all()
user1 = Table(username='User1')
db.session.add(user1)
db.session.commit()

警告你必须调试这个......我只使用 Flask + SQLAlchemy。


推荐阅读