如何在duckdb中更改数据约束的代码部分。

huangapple go评论78阅读模式
英文:

How to alter data constraint in duckdb R

问题

I'm here to help with the translation:

我正在尝试在duckdb(R API)中将 Not Null 约束修改为 Null 约束,但无法成功。以下是问题的示例:

drv <- duckdb()
con <- dbConnect(drv)
dbExecute(con, "CREATE TABLE db(a varchar(1) NOT NULL, b varchar(1) NOT NULL)")

d <- data.table(a = 1:3, b = c("a", NA, "b"))

# 测试 'not null' 约束
dbWriteTable(con, name = "db", value = d, append = TRUE)
Error: Constraint Error: NOT NULL constraint failed: db.b

在这里,我试图修改 'db' 以允许我在列 b 中写入具有 NA 值的 'd'。

> dbSendQuery(con, "ALTER TABLE db ALTER COLUMN b SET DEFAULT NULL")
<duckdb_result 46970 connection=b6ff0 statement='ALTER TABLE db ALTER COLUMN b SET DEFAULT NULL'>

> dbWriteTable(con, name = "db", value = d, append = TRUE)
Error: Constraint Error: NOT NULL constraint failed: db.b

似乎 'dbSendQuery' 用于修改表格(至少没有错误),但很明显约束并未更改。对于如何使约束更改生效,有任何想法吗?

英文:

I am trying to alter a Not Null constraint to a Null constraint in duckdb (R api) and can't get it to stick. Here is an example of the problem.

drv&lt;- duckdb()
con&lt;- dbConnect(drv)
dbExecute(con, &quot;CREATE TABLE db(a varchar(1) NOT NULL, b varchar(1) NOT NULL)&quot;)

d&lt;- data.table(a = 1:3, b=c(&quot;a&quot;, NA, &quot;b&quot;))

#test &#39;not null&#39; constraint
dbWriteTable(con, name = &quot;db&quot;, value = d, append = T)
Error: Constraint Error: NOT NULL constraint failed: db.b

So here I try and alter 'db' to allow me to write 'd' with its NA value in column b

&gt; dbSendQuery(con, &quot;ALTER TABLE db ALTER COLUMN b SET DEFAULT NULL&quot;)
&lt;duckdb_result 46970 connection=b6ff0 statement=&#39;ALTER TABLE db ALTER COLUMN b SET DEFAULT NULL&#39;&gt;

&gt; dbWriteTable(con, name = &quot;db&quot;, value = d, append = T)
Error: Constraint Error: NOT NULL constraint failed: db.b

It appears the 'dbSendQuery' to alter the table worked (no error at least), but its clear that the constraint was not changed. Any ideas on how to make the constraint change stick?

答案1

得分: 2

使用 DROP NOT NULL 而不是 SET NOT NULL

DBI::dbExecute(duck, "ALTER TABLE db ALTER COLUMN b DROP NOT NULL")
# [1] 0
DBI::dbWriteTable(duck, name = "db", value = d, append = T)
DBI::dbGetQuery(duck, "select * from db")
#   a    b
# 1 1    a
# 2 2 <NA>
# 3 3    b
英文:

Use DROP NOT NULL instead of SET NOT NULL.

d &lt;- data.table(a = 1:3, b=c(&quot;a&quot;, NA, &quot;b&quot;))DBI::dbWriteTable(duck, name = &quot;db&quot;, value = d, append = T)
duck &lt;- DBI::dbConnect(duckdb::duckdb())
DBI::dbWriteTable(duck, name = &quot;db&quot;, value = d, append = T)
# ERROR [2023-05-13 14:11:15] {&quot;msg&quot;:&quot;uncaught error&quot;,&quot;error&quot;:&quot;rapi_execute: Failed to run query\nError: Constraint Error: NOT NULL constraint failed: db.b&quot;,&quot;where&quot;:[&quot;DBI::dbWriteTable(duck, name = \&quot;&quot;,&quot;DBI::dbWriteTable(duck, name = \&quot;&quot;,&quot;.local(conn, name, value, ...)&quot;],&quot;pid&quot;:&quot;511449&quot;}
# Error: rapi_execute: Failed to run query
# Error: Constraint Error: NOT NULL constraint failed: db.b

Fixed:

DBI::dbExecute(duck, &quot;ALTER TABLE db ALTER COLUMN b DROP NOT NULL&quot;)
# [1] 0
DBI::dbWriteTable(duck, name = &quot;db&quot;, value = d, append = T)
DBI::dbGetQuery(duck, &quot;select * from db&quot;)
#   a    b
# 1 1    a
# 2 2 &lt;NA&gt;
# 3 3    b

huangapple
  • 本文由 发表于 2023年5月13日 10:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定