go-sqlite3
go-sqlite3 copied to clipboard
When use transaction, "no such table" error will occur
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)
}
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