mattn / mattn/go-sqlite3

Error "no such table" when creating temp tables

Open
#1,176 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
9.2k
Forks
1.2k
Avg merge
19m
Merged PRs (30d)
4

Description

When I create a temp table, it throws an error that the table doesn't exist. When running the commands in datagrip and the sqlite cli with parameters replaced, it does not error. As long as I don't use the temp table (joins or selects) then it doesn't error in the go code. I believe this is an issue with go-sqlite3 or some configuration that I can't figure out because when I test it with github.com/glebarez/sqlite the query executes with no errors.

Actual application query

DROP TABLE IF EXISTS deleted_watchtimes;
CREATE TEMP TABLE deleted_watchtimes (
    row_id INTEGER
);

WITH cte AS (SELECT value FROM JSON_EACH(:names))
INSERT
INTO deleted_watchtimes
SELECT w._ROWID_
FROM watch_time AS w
JOIN user u ON w.user_id = u.id
WHERE u.name IN cte
  AND w.deleted = 0;

UPDATE watch_time
SET deleted = TRUE
WHERE _ROWID_ IN deleted_watchtimes;

SELECT DISTINCT u.name
FROM watch_time w
JOIN user u ON w.user_id = u.id
WHERE w._ROWID_ IN deleted_watchtimes;

Testing sql query

DROP TABLE IF EXISTS deleted_watchtimes;
CREATE TEMP TABLE deleted_watchtimes (row_id INTEGER);

WITH cte AS (SELECT value FROM JSON_EACH(:names))
INSERT
INTO deleted_watchtimes
SELECT *
FROM cte;

SELECT * FROM deleted_watchtimes

Test code that errors

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/mattn/go-sqlite3"
	// _ "github.com/glebarez/sqlite"
)

func main() {
	// test.db does not exist
	db, err := sql.Open("sqlite3", "test.db")
	// db, err := sql.Open("sqlite", "test.db")
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	defer db.Close()

	rows, err := db.Query(`
	DROP TABLE IF EXISTS deleted_watchtimes;
	CREATE TEMP TABLE deleted_watchtimes (row_id INTEGER);
	
	WITH cte AS (SELECT value FROM JSON_EACH(:names))
	INSERT
	INTO deleted_watchtimes
	SELECT *
	FROM cte;

	SELECT * FROM deleted_watchtimes`, sql.Named("names", `["a","b"]`))
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	defer rows.Close()

	for rows.Next() {
		var res string
		err := rows.Scan(&res)
		if err != nil {
			fmt.Printf("error: %v\n", err)
			return
		}
		fmt.Println(res)
	}
}

If I use the sql query below, it doesn't error

DROP TABLE IF EXISTS deleted_watchtimes;
CREATE TEMP TABLE deleted_watchtimes (row_id INTEGER);

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal main program in the issue and compare the failing database/sql Query call with the two-statement query that succeeds. Trace how the driver handles the temporary table across the supplied statements. Done means the reproduction no longer reports the missing table and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sqlite
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.