glebarez / sqlite

The pure-Go SQLite driver for GORM
MIT License
592 stars 39 forks source link

Generates a wrong DDL #94

Closed aymanbagabas closed 5 months ago

aymanbagabas commented 1 year ago

I'm still new to GORM and have been using modernc.org/sqlite for quite a while.

When I try to rerun the migrations I get the following error for these models:


// Model is the base model for all models.
type Model struct {
    ID        uint           `gorm:"primaryKey"`
    CreatedAt time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP"`
    UpdatedAt time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP"`
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

// PublicKey is a public key model.
type PublicKey struct {
    Model
    Key    string `gorm:"column:public_key;index:idx_public_key_key,unique;index:idx_public_key_user,unique;not null"`
    UserID uint   `gorm:"index:idx_public_key_user,unique;not null"`
    User   User   `gorm:"constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"`
}

// TableName returns the table name for the public key model.
func (PublicKey) TableName() string {
    return "public_key"
}
2023/07/05 16:07:27 /Users/ayman/.go/pkg/mod/github.com/glebarez/sqlite@v1.8.0/migrator.go:406 SQL logic error: near "text": syntax error (1)
[0.014ms] [rows:0] CREATE TABLE `public_key__temp` text NOT NULL UNIQUE,`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`deleted_at` datetime,`public_key` ?,`user_id` integer NOT NULL,PRIMARY KEY (`id`),CONSTRAINT `fk_user_public_key` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE)

I'm assuming the error is happening because I'm using the same name for the column public_key and table name public_key

glebarez commented 1 year ago

Hello, does this reproduce using the GORM standard driver?