import (
"github.com/vogo/duckdb"
"gorm.io/gorm"
)
type Product struct {
ID uint `gorm:"primarykey"`
Code string
Price uint
CreatedAt time.Time
UpdatedAt time.Time
}
func main() {
db, err := gorm.Open(duckdb.Open("duckdb.ddb"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&Product{})
// Create
db.Create(&Product{Code: "D42", Price: 100})
// Read
var product Product
db.First(&product, 1)
db.First(&product, "code = ?", "D42")
// Update
db.Model(&product).Update("Price", 200)
db.Model(&product).Updates(Product{Price: 200, Code: "F42"})
// Delete
db.Delete(&product, 1)
}
Checkout https://gorm.io for details.
Do not use gorm.Model
- use custom struct with ID
, CreatedAt
, UpdatedAt
fields instead.
DuckDB's ART indexes have limitations with soft deletes. When GORM performs db.Delete()
, it updates the deleted_at
field instead of actually deleting the record, which can cause primary key constraint violations due to how DuckDB handles transactions and indexes.
See DuckDB documentation for details.
Any contributions you make are greatly appreciated.
This project is licensed under the Apache License 2.0.