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

Autoincrement is not enabled in SQLite when used with AutoMigrate #4760

Closed
iSLC opened this issue Oct 10, 2021 · 9 comments · Fixed by #6624
Closed

Autoincrement is not enabled in SQLite when used with AutoMigrate #4760

iSLC opened this issue Oct 10, 2021 · 9 comments · Fixed by #6624
Assignees
Labels
type:missing reproduction steps missing reproduction steps

Comments

@iSLC
Copy link

iSLC commented Oct 10, 2021

Description

Very simple use case:

package main

import (
	"log"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type User struct {
	ID   int64  `gorm:"primaryKey;autoIncrement;not null"`
	Name string `gorm:"unique;not null"`
	Mail string `gorm:"unique;not null"`
	Pass string `gorm:"unique;not null"`
}

func main() {
	con, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		log.Panic(err)
	}
	con.AutoMigrate(User{})
}

The database and table is created. Everything else is fine. Except AUTOINCREMENT is not enabled on primary key. What am I doing wrong here? (I have not tried with other drivers) (just tried on mysql and works fine there)

This is the DDL of the generated table:

CREATE TABLE users (
    id   INTEGER NOT NULL,
    name TEXT    NOT NULL
                 UNIQUE,
    mail TEXT    NOT NULL
                 UNIQUE,
    pass TEXT    NOT NULL
                 UNIQUE,
    PRIMARY KEY (
        id
    )
);

OS: Windows x64
GO: 1.17.2 (fresh installation. just began learning go)

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Oct 10, 2021
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@ghost
Copy link

ghost commented Oct 11, 2021

@iSLC hello, a field declared as INTEGER PRIMARY KEY will automatically add in sqite doc

look here and it explain this issue.
https://github.com/go-gorm/sqlite/blob/2a8115147a2b4d95eb4455a253de09bb19a11fc0/sqlite.go#L146

@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

@iSLC
Copy link
Author

iSLC commented Oct 11, 2021

@longlihale So basically i only need to specify autoIncrement without primaryKey? At least that's what I can infer from the code at first glance. nvm I just read it wrong.

case schema.Int, schema.Uint:
		if field.AutoIncrement && !field.PrimaryKey {
			// https://www.sqlite.org/autoinc.html
			return "integer PRIMARY KEY AUTOINCREMENT"
		} else {
			return "integer"
		}

I'll have to look more into it when I get home. Thanks for the reply.

@ghost ghost mentioned this issue Oct 11, 2021
@ghost ghost closed this as completed Oct 11, 2021
@ghost
Copy link

ghost commented Oct 11, 2021

@longlihale So basically i only need to specify autoIncrement without primaryKey? At least that's what I can infer from the code at first glance. nvm I just read it wrong.

case schema.Int, schema.Uint:
		if field.AutoIncrement && !field.PrimaryKey {
			// https://www.sqlite.org/autoinc.html
			return "integer PRIMARY KEY AUTOINCREMENT"
		} else {
			return "integer"
		}

I'll have to look more into it when I get home. Thanks for the reply.

OK

@yqchilde
Copy link

yqchilde commented May 7, 2022

If you want to set the auto-increment on the ID field, using the autoIncrement tag alone does not take effect, because the primaryKey is added to the ID by default, which makes it impossible to set the auto-increment on the ID field anyway.

@Binary-Eater
Copy link

If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.

The above is from the SQLite docs. I think, given the docs, the logic in go-gorm/sqlite3 should be field.AutoIncrement && field.PrimaryKey instead of what it is currently.

@Binary-Eater
Copy link

Actually, one can use autoIncrement -> INTEGER PRIMARY KEY AUTOINCREMENT with sqlite3, so the current behavior makes sense to me.

@onlyshow
Copy link

How was it finally resolved?

samuelncui added a commit to samuelncui/gorm that referenced this issue Oct 4, 2023
jinzhu pushed a commit that referenced this issue Oct 9, 2023
…6624)

* fix: sqlite dialector cannot apply `PRIMARY KEY AUTOINCREMENT` type

fix #4760

* feat: add auto increment test

* feat: update sqlite

* feat: update tests deps sqlite to v1.5.4
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:missing reproduction steps missing reproduction steps
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants