DuckDB Binder错误:FROM子句中未找到引用的列

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

DuckDB Binder Error: Referenced column not found in FROM clause

问题

  1. 我在DuckDB中处理从json中读取的数据库
  2. 这是json
  3. ```json
  4. [{
  5. "account": "abcde",
  6. "data": [
  7. {
  8. "name": "hey",
  9. "amount": 1,
  10. "flow": "INFLOW"
  11. },
  12. {
  13. "name": "hello",
  14. "amount": -2,
  15. "flow": null
  16. }
  17. ]
  18. },
  19. {
  20. "account": "hijkl",
  21. "data": [
  22. {
  23. "name": "bonjour",
  24. "amount": 1,
  25. "flow": "INFLOW"
  26. },
  27. {
  28. "name": "hallo",
  29. "amount": -3,
  30. "flow": "OUTFLOW"
  31. }
  32. ]
  33. }
  34. ]

我在Python中这样打开它:

  1. import duckdb
  2. duckdb.sql("""
  3. CREATE OR REPLACE TABLE mytable AS SELECT * FROM "example2.json"
  4. """)

这一切都很顺利,我得到了我的表的副本,但是然后我尝试更新它:

  1. duckdb.sql("""
  2. UPDATE mytable SET data = NULL WHERE account = "abcde"
  3. """)

这导致崩溃:

  1. ---------------------------------------------------------------------------
  2. BinderException Traceback (most recent call last)
  3. Cell In[109], line 1
  4. ----> 1 duckdb.sql("""
  5. 2 UPDATE mytable SET data = NULL WHERE account = "abcde"
  6. 3 """)
  7. 6 # duckdb.sql("""
  8. 7 # DELETE FROM mytable WHERE account = "abcde"
  9. 8 # """)
  10. 10 duckdb.sql("""
  11. 11 SELECT * FROM mytable
  12. 12 """)
  13. BinderException: Binder Error: Referenced column "abcde" not found in FROM clause!
  14. Candidate bindings: "mytable.data"
  15. LINE 2: ...mytable SET data = NULL WHERE account = "abcde"
  16. ^

我已经搜索了文档和错误,但我就是找不到我在这里做错了什么。

  1. <details>
  2. <summary>英文:</summary>
  3. I am working in DuckDB in a database that I read from json.
  4. Here is the json:
  5. ```json
  6. [{
  7. &quot;account&quot;: &quot;abcde&quot;,
  8. &quot;data&quot;: [
  9. {
  10. &quot;name&quot;: &quot;hey&quot;,
  11. &quot;amount&quot;:1,
  12. &quot;flow&quot;:&quot;INFLOW&quot;
  13. },
  14. {
  15. &quot;name&quot;: &quot;hello&quot;,
  16. &quot;amount&quot;:-2,
  17. &quot;flow&quot;: null
  18. }
  19. ]
  20. },
  21. {
  22. &quot;account&quot;: &quot;hijkl&quot;,
  23. &quot;data&quot;: [
  24. {
  25. &quot;name&quot;: &quot;bonjour&quot;,
  26. &quot;amount&quot;:1,
  27. &quot;flow&quot;:&quot;INFLOW&quot;
  28. },
  29. {
  30. &quot;name&quot;: &quot;hallo&quot;,
  31. &quot;amount&quot;:-3,
  32. &quot;flow&quot;:&quot;OUTFLOW&quot;
  33. }
  34. ]
  35. }
  36. ]

I am opening it in Python as follows:

  1. import duckdb
  2. duckdb.sql(&quot;&quot;&quot;
  3. CREATE OR REPLACE TABLE mytable AS SELECT * FROM &quot;example2.json&quot;
  4. &quot;&quot;&quot;)

This all works fine and I get a copy of my table, but then I try to update it:

  1. duckdb.sql(&quot;&quot;&quot;
  2. UPDATE mytable SET data = NULL WHERE account = &quot;abcde&quot;
  3. &quot;&quot;&quot;)

which crashes with

  1. ---------------------------------------------------------------------------
  2. BinderException Traceback (most recent call last)
  3. Cell In[109], line 1
  4. ----&gt; 1 duckdb.sql(&quot;&quot;&quot;
  5. 2 UPDATE mytable SET data = NULL WHERE account = &quot;abcde&quot;
  6. 3 &quot;&quot;&quot;)
  7. 6 # duckdb.sql(&quot;&quot;&quot;
  8. 7 # DELETE FROM mytable WHERE account = &quot;abcde&quot;
  9. 8 # &quot;&quot;&quot;)
  10. 10 duckdb.sql(&quot;&quot;&quot;
  11. 11 SELECT * FROM mytable
  12. 12 &quot;&quot;&quot;)
  13. BinderException: Binder Error: Referenced column &quot;abcde&quot; not found in FROM clause!
  14. Candidate bindings: &quot;mytable.data&quot;
  15. LINE 2: ...mytable SET data = NULL WHERE account = &quot;abcde&quot;
  16. ^

I have searched the documentation and the error but I just can't find what I am doing wrong here.

答案1

得分: 1

I actually solved the issue. I had to use single quotes ' instead of double quotes " in the string comparison...

Solution

  1. duckdb.sql('''
  2. UPDATE mytable SET data = NULL WHERE account = 'abcde'
  3. ''')

correctly does

  1. ┌─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────┐
  2. account data
  3. varchar struct("name" varchar, amount bigint, flow varchar)[]
  4. ├─────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
  5. hijkl [{'name': bonjour, 'amount': 1, 'flow': INFLOW}, {'name': hallo, 'amount': -3, 'flow': OUTFLOW}]
  6. abcde NULL
  7. └─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────┘

Interestingly, ChatGPT helped me spot this mistake. (There is a ban on posting AI answers, but it's OK if they are human-verified).

英文:

I actually solved the issue. I had to use single quotes &#39; instead of double quotes &quot; in the string comparison...

Solution

  1. duckdb.sql(&quot;&quot;&quot;
  2. UPDATE mytable SET data = NULL WHERE account = &#39;abcde&#39;
  3. &quot;&quot;&quot;)

correctly does

  1. ┌─────────┬──────────────────────────────────────────────────────────────────────────────────────────────────┐
  2. account data
  3. varchar struct(&quot;name&quot; varchar, amount bigint, flow varchar)[]
  4. ├─────────┼──────────────────────────────────────────────────────────────────────────────────────────────────┤
  5. hijkl [{&#39;name&#39;: bonjour, &#39;amount&#39;: 1, &#39;flow&#39;: INFLOW}, {&#39;name&#39;: hallo, &#39;amount&#39;: -3, &#39;flow&#39;: OUTFLOW}]
  6. abcde NULL
  7. └─────────┴──────────────────────────────────────────────────────────────────────────────────────────────────┘

Interestingly, ChatGPT helped me spot this mistake. (There is a ban on posting AI answers, but it's OK if they are human-verified).

答案2

得分: 0

在处理 DuckDB 中的 varchar 时,请使用单引号 '' 而不是双引号 ""。

英文:

When dealing with varchar in DuckDB, use single quotes '' instead of double quotes "".

huangapple
  • 本文由 发表于 2023年3月8日 17:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671499.html
匿名

发表评论

匿名网友

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

确定