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 ignored when combined with primaryKey #134

Open
pboguslawski opened this issue Feb 24, 2023 · 2 comments
Open

autoIncrement ignored when combined with primaryKey #134

pboguslawski opened this issue Feb 24, 2023 · 2 comments
Assignees

Comments

@pboguslawski
Copy link

Description

According to
https://www.sqlitetutorial.net/sqlite-autoincrement/
https://www.sqlite.org/autoinc.html
AUTOINCREMENT should be used in sqlite if one want's ID values not to be reused (i.e. after row deletion).

With table definition

ID uint64   `gorm:"column:id;not null;primaryKey;autoIncrement"`

GORM creates column without autoincrement:

CREATE TABLE `[...]` (`id` integer NOT NULL,[...],PRIMARY KEY (`id`));

but should add autoincrement also.

According to #126 one can create primary key + autoincrement with autoIncrement tag only but this looks like dirty hack and error

failed to create database schema: table "[...]" has more than one primary key

is thrown when trying to create table with

	IDH uint64 `gorm:"column:idh;not null;autoIncrement"` // Tried to create as PK+autoincrement.
	ID  uint64 `gorm:"column:id;not null"`                 // Tried to create as foreign key with ID name (makes sense in history table for us).

because GORM tries ID as PK by default (because of col name) and adds PK also to IDH (because of wrong handling of tags like above).

Expected behaviour

Column defined with gorm:"[...];primaryKey;autoIncrement" should be created as PK with autoincrement in sqlite db.

@dvdlevanon
Copy link

dvdlevanon commented Mar 26, 2023

I have the same issue, it causes weird bugs because IDs get reuse.

@championswimmer
Copy link

@dvdlevanon @pboguslawski
This has been fixed in 1.25.5
go-gorm/gorm@2095d42

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

4 participants