python - 将数据框复制到具有默认值的列的 postgres 表
问题描述
我有以下 postgreSql 表stock
,结构如下,列insert_time
有一个默认值now()
| column | pk | type |
+-------------+-----+-----------+
| id | yes | int |
| type | yes | enum |
| c_date | | date |
| qty | | int |
| insert_time | | timestamp |
我正在尝试copy
以下df
| id | type | date | qty |
+-----+------+------------+------+
| 001 | CB04 | 2015-01-01 | 700 |
| 155 | AB01 | 2015-01-01 | 500 |
| 300 | AB01 | 2015-01-01 | 1500 |
我psycopg
用来上传df
到表stock
cur.copy_from(df, stock, null='', sep=',')
conn.commit()
收到此错误。
DataError: missing data for column "insert_time"
CONTEXT: COPY stock, line 1: "001,CB04,2015-01-01,700"
我期待使用 psycopg copy_from 函数,我的 postgresql 表将自动填充插入时间旁边的行。
| id | type | date | qty | insert_time |
+-----+------+------------+------+---------------------+
| 001 | CB04 | 2015-01-01 | 700 | 2018-07-25 12:00:00 |
| 155 | AB01 | 2015-01-01 | 500 | 2018-07-25 12:00:00 |
| 300 | AB01 | 2015-01-01 | 1500 | 2018-07-25 12:00:00 |
解决方案
您可以像这样指定列:
cur.copy_from(df, stock, null='', sep=',', columns=('id', 'type', 'c_date', 'qty'))
推荐阅读
- swift - 如何将标签的文本设置为 Firebase 中的数据
- reactjs - 如何在 saga 中测试 firebase 功能?
- reactjs - 如何在 reactjs 的 CKEditor5 工具栏中添加下划线选项和对齐选项
- javascript - 获取触发事件的复选框的 ID
- django - 可以将用户/配置文件链接到先前创建的名称,该名称通过外键链接到模型
- spyder - 如何在 Spyder 4 中禁用代码折叠?
- python - 使用烧瓶更新 Web 应用程序中的变量
- java - TimerTask Scheduler 在静态引用其他类对象后不起作用
- swift - 如何在 Swift 中嵌入文件?
- c++ - 使用 `emplace_back` 而不是 `push_back` 时没有缩小警告