Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generates a wrong DDL #94

Closed
aymanbagabas opened this issue Jul 5, 2023 · 1 comment
Closed

Generates a wrong DDL #94

aymanbagabas opened this issue Jul 5, 2023 · 1 comment

Comments

@aymanbagabas
Copy link

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
Copy link
Owner

glebarez commented Jul 6, 2023

Hello,
does this reproduce using the GORM standard driver?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants