Skip to content

Cannot set primary key ID when id type is int or int64 #586

@sebd71

Description

@sebd71

Hello,

with a postgres connection (same issue at least with sqlite), if ID field is type int or int64 (ID int 'db:"id"' ); calling tx.Create with a preset ID is not possible. ID will be overwritten by create method. ( see dialect_postgresql.go#L64 and dialect_postgresql.go#L87 )

Is it possible to change and have the same behavior as UUID ?

if model.ID() == emptyUUID {

A possible fix, would be to add a check like :

	switch keyType {
	case "int", "int64":
            if model.ID() != 0 {
               // keep current logic that allocate a new id value
            }

Here is a testcase that can be added to executors_test.go file that illustrate this problem :

func Test_Create_Single_Set_ID(t *testing.T) {
	if PDB == nil {
		t.Skip("skipping integration tests")
	}
	r := require.New(t)
	validationLogs = []string{}
	transaction(func(tx *Connection) {
		singleID := &SingleID{
			ID: 123456,
		}
		err := tx.Create(singleID)
		r.NoError(err)
		r.Equal(123456, singleID.ID)
	})
}

And here is the test output:

--- FAIL: Test_Create_Single_Set_ID (0.00s)
    executors_test.go:185: 
                Error Trace:    executors_test.go:185
                                                        pop_test.go:66
                                                        connection.go:166
                                                        pop_test.go:65
                                                        executors_test.go:179
                Error:          Not equal: 
                                expected: 123456
                                actual  : 1
                Test:           Test_Create_Single_Set_ID
FAIL
exit status 1
FAIL    github.com/gobuffalo/pop/v5     0.043s

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeThis feature / fix introduces breaking changesproposalA suggestion for a change, feature, enhancement, etc

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions