marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
623 stars 97 forks source link

Cannot create tables with Primary Key (silent fail) #178

Closed LaughingBubba closed 6 months ago

LaughingBubba commented 6 months ago

v0.9.2 3c695d7ba9

Trying the create a table with single or compound primary keys fails silently.

When you try and open the DB you get the following:

Error: unable to open database "data/aec29422.db": Serialization Error: Failed to deserialize: expected end of object, but found field id: 104

I'm just initialising the DB with a table thus:

  dbName := "./data/aec29422.db"

  db, err := sql.Open("duckdb", dbName)
  if err != nil {
    log.Fatal(err)
  }
  defer db.Close()

  _, err = db.Exec(`DROP TABLE IF EXISTS candidateFP;`)
  if err != nil {
    log.Fatalf("drop failed", err)
  }
  _, err = db.Exec(`
    CREATE TABLE candidateFP (
      cyc_created   VARCHAR,
      evt_id        BIGINT,
      evt_name      VARCHAR,
      cnt_id        BIGINT,
      cnt_name      VARCHAR,
      cnt_updated   VARCHAR,
      cnt_declared  VARCHAR,
      res_phase     VARCHAR,
      pod_id        BIGINT,
      pod_code      VARCHAR,
      pod_name      VARCHAR,
      pod_state     VARCHAR,
      c_id          BIGINT,
      c_name        VARCHAR,
      c_party       VARCHAR,
      c_votes       BIGINT,
      c_balPos      BIGINT,
      c_type        VARCHAR,
      PRIMARY KEY (cyc_created, evt_id, cnt_id, pod_id, c_id)
    );
  `)
  if err != nil {
    log.Fatalf("create failed", err)
  }

Like wise, even something as simple as this won't work:

  _, err = db.Exec(`
    CREATE TABLE candidateFP (
      cyc_created   DATE PRIMARY KEY,
      evt_id        BIGINT,
      evt_name      VARCHAR,
      cnt_id        BIGINT,
      cnt_name      VARCHAR,
      cnt_updated   DATE,
      cnt_declared  DATE,
      res_phase     VARCHAR,
      pod_id        BIGINT,
      pod_code      VARCHAR,
      pod_name      VARCHAR,
      pod_state     VARCHAR,
      c_id          BIGINT,
      c_name        VARCHAR,
      c_party       VARCHAR,
      c_votes       BIGINT,
      c_balPos      BIGINT,
      c_type        VARCHAR
    );
  `)

If you copy+paste into DuckDB console, they will work, so it's defs not a syntax problem.

If there is NO primary key defined, the table gets created as expected:

  _, err = db.Exec(`
    CREATE TABLE candidateFP (
      cyc_created   DATE,
      evt_id        BIGINT,
      evt_name      VARCHAR,
      cnt_id        BIGINT,
      cnt_name      VARCHAR,
      cnt_updated   DATE,
      cnt_declared  DATE,
      res_phase     VARCHAR,
      pod_id        BIGINT,
      pod_code      VARCHAR,
      pod_name      VARCHAR,
      pod_state     VARCHAR,
      c_id          BIGINT,
      c_name        VARCHAR,
      c_party       VARCHAR,
      c_votes       BIGINT,
      c_balPos      BIGINT,
      c_type        VARCHAR
    );
  `)
LaughingBubba commented 6 months ago

After half a day tearing my hair out, I thought I'd do a brew upgrade duckdb and lo, this problem is resolved inv0.10.0 20b1486d11