go-gorm / go-gorm/sqlite

autoIncrement ignored when combined with primaryKey

Open
#134 3 comments 1 reaction 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
231
Forks
217
PR merge metrics
No merged PRs in 30d

Description

## 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 https://github.com/go-gorm/sqlite/issues/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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.