go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

When use transaction, "no such table" error will occur

Open wenzhiquan opened this issue 5 years ago • 1 comments

When I use transaction, "no such table" error will occur. The following is my test code:

package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"

)

type TestEntity struct {
	Name string   `gorm:"name"`
	Type string   `gorm:"type"`
}

func main() {
	db, err := gorm.Open("sqlite3", ":memory:?cache=shared")
	if err != nil {
		panic(err)
	}
	db.LogMode(true)
	db.CreateTable(&TestEntity{})
	temp := &TestEntity{
		Name: "test",
		Type: "test",
	}
	err = db.Create(temp).Error
	if err != nil {
		panic(err)
	}
	query := db.Where("name=?", "test")
	var result TestEntity
	err = query.First(&result).Error
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
	tx := db.Begin()
	query = db.Where("name=?", "test")
	err = query.First(&result).Error
	if err != nil {
		panic(err)
	}
	tx.Commit()
	fmt.Println(result)
}

wenzhiquan avatar Jun 10 '20 09:06 wenzhiquan

this is not an issue with mattn/go-sqlite3. On starting a new transaction, a separate (new or cached ) connection is requested by database/sql and the dns = ":memory:?cache=shared" creates a new (fresh) in-memory database. To use the same in-memory database set uri qualified dns e.g. file::memory:?cache=shared or something better like "file:yourDbName?mode=memory&cache=shared". PS mattn/go-sqlite3 treats dns as uri if it contains ? but sqlite3 treat filename as uri only if it is according to RFC 3986

dikshek99 avatar Jun 22 '20 18:06 dikshek99