python - 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.
我检查了一些帖子并尝试了他们的解决方案但仍然无法正常工作
解决方案
尝试这个:
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。
推荐阅读
- c++ - 将指针传递给 std::function
- php - 自动创建 Laravel 项目失败并出现错误:TTY 模式需要 /dev/tty 可以读/写
- angular - 无法选择 Angular Material 芯片/芯片并表明该芯片已被选中?
- python-3.x - Pandas 在 groupby 之后移动多列导致按字母顺序移动列
- xero-api - 联系方式 - 默认货币
- javascript - 使用 Lodash 和 Moment 从单个日期开始的日/小时分组数组
- python-3.x - 使用 Python 将 sigmoid 函数(“S”形曲线)拟合到数据中
- python - 如何使用 sklearn 或 scipy 聚类方法打印聚类的层次结构?
- r - 在 Amazon AWS Ec2 上运行 RStudio
- jquery - 当没有帖子时,为什么计数器在第 1 篇帖子中增加 n 次