Merge pull request #210 from rangzen/fix-postgres-mutablecolumns-isgenerated
fix generated columns in mutable columns for PostgreSQL
This commit is contained in:
commit
ec3adb3d88
6 changed files with 81 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ type Column struct {
|
|||
Name string
|
||||
IsPrimaryKey bool
|
||||
IsNullable bool
|
||||
IsGenerated bool
|
||||
DataType DataType
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ func (t Table) MutableColumns() []Column {
|
|||
var ret []Column
|
||||
|
||||
for _, column := range t.Columns {
|
||||
if column.IsPrimaryKey {
|
||||
if column.IsPrimaryKey || column.IsGenerated {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ WITH primaryKeys AS (
|
|||
)
|
||||
SELECT column_name as "column.Name",
|
||||
is_nullable = 'YES' as "column.isNullable",
|
||||
is_generated = 'ALWAYS' or is_generated = 'YES' as "column.isGenerated",
|
||||
(EXISTS(SELECT 1 from primaryKeys as pk where pk.column_name = columns.column_name)) as "column.IsPrimaryKey",
|
||||
dataType.kind as "dataType.Kind",
|
||||
(case dataType.Kind when 'base' then data_type else LTRIM(udt_name, '_') end) as "dataType.Name",
|
||||
|
|
|
|||
|
|
@ -573,7 +573,7 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
testutils.AssertFileNamesEqual(t, modelFiles, "all_types.go", "all_types_view.go", "employee.go", "link.go",
|
||||
"mood.go", "person.go", "person_phone.go", "weird_names_table.go", "level.go", "user.go", "floats.go")
|
||||
"mood.go", "person.go", "person_phone.go", "weird_names_table.go", "level.go", "user.go", "floats.go", "people.go")
|
||||
|
||||
testutils.AssertFileContent(t, modelDir+"/all_types.go", allTypesModelContent)
|
||||
|
||||
|
|
@ -581,7 +581,7 @@ func TestGeneratedAllTypesSQLBuilderFiles(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
testutils.AssertFileNamesEqual(t, tableFiles, "all_types.go", "employee.go", "link.go",
|
||||
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "table.go")
|
||||
"person.go", "person_phone.go", "weird_names_table.go", "user.go", "floats.go", "table.go", "people.go")
|
||||
|
||||
testutils.AssertFileContent(t, tableDir+"/all_types.go", allTypesTableContent)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -476,3 +476,78 @@ FROM test_sample."User";
|
|||
]
|
||||
`)
|
||||
}
|
||||
|
||||
func TestMutableColumnsExcludeGeneratedColumn(t *testing.T) {
|
||||
// clean up
|
||||
_, err := People.DELETE().WHERE(People.PeopleID.GT(Int(3))).Exec(db)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("should not have the generated column in mutableColumns", func(t *testing.T) {
|
||||
require.Equal(t, 2, len(People.MutableColumns))
|
||||
require.Equal(t, People.PeopleName, People.MutableColumns[0])
|
||||
require.Equal(t, People.PeopleHeightCm, People.MutableColumns[1])
|
||||
})
|
||||
|
||||
t.Run("should query with all columns", func(t *testing.T) {
|
||||
query := SELECT(
|
||||
People.AllColumns,
|
||||
).FROM(
|
||||
People,
|
||||
).WHERE(
|
||||
People.PeopleID.EQ(Int(3)),
|
||||
)
|
||||
|
||||
var result model.People
|
||||
|
||||
err := query.Query(db, &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "Carla", result.PeopleName)
|
||||
require.Equal(t, 155., *result.PeopleHeightCm)
|
||||
require.InEpsilon(t, 61.02, *result.PeopleHeightIn, 1e-3)
|
||||
})
|
||||
|
||||
t.Run("should insert without generated columns", func(t *testing.T) {
|
||||
insertQuery := People.INSERT(
|
||||
People.MutableColumns,
|
||||
).MODEL(
|
||||
model.People{
|
||||
PeopleName: "Dario",
|
||||
PeopleHeightCm: testutils.Float64Ptr(120),
|
||||
},
|
||||
).RETURNING(
|
||||
People.MutableColumns,
|
||||
)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, insertQuery, `
|
||||
INSERT INTO test_sample.people (people_name, people_height_cm)
|
||||
VALUES ('Dario', 120)
|
||||
RETURNING people.people_name AS "people.people_name",
|
||||
people.people_height_cm AS "people.people_height_cm";
|
||||
`)
|
||||
|
||||
var result model.People
|
||||
err := insertQuery.Query(db, &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "Dario", result.PeopleName)
|
||||
require.Equal(t, 120., *result.PeopleHeightCm)
|
||||
|
||||
query := SELECT(
|
||||
People.AllColumns,
|
||||
).FROM(
|
||||
People,
|
||||
).ORDER_BY(
|
||||
People.PeopleID.DESC(),
|
||||
).LIMIT(1)
|
||||
|
||||
result = model.People{}
|
||||
|
||||
err = query.Query(db, &result)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "Dario", result.PeopleName)
|
||||
require.Equal(t, 120., *result.PeopleHeightCm)
|
||||
require.InEpsilon(t, 47.24, *result.PeopleHeightIn, 1e-3)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit fdb0cc598d2b534310d2b559ce9a2f75b5507c56
|
||||
Subproject commit c85dd20a33d48df3527acaaf8fdbcb6b3d3a4b57
|
||||
Loading…
Add table
Add a link
Reference in a new issue