diff --git a/db_information.go b/db_information.go index f0f6bb1..cffb37c 100644 --- a/db_information.go +++ b/db_information.go @@ -225,6 +225,11 @@ func (db *DB) CheckSchema(ctx context.Context) error { return fmt.Errorf("column %q in table %q not found in schema", col.Name, tableName) } + if column.Unique { // modify it, so checks are correct. + column.UniqueIndex = fmt.Sprintf("%s_%s_key", tableName, column.Name) + column.Unique = false + } + if expected, got := col.FieldTagString(false), column.FieldTagString(false); expected != got { return fmt.Errorf("column %q in table %q has wrong field tag: db:\n%s\nvs code:\n%s", col.Name, tableName, expected, got) } diff --git a/desc/column.go b/desc/column.go index 94747dc..8f3d25b 100644 --- a/desc/column.go +++ b/desc/column.go @@ -144,8 +144,10 @@ func (c *Column) FieldTagString(strict bool) string { writeTagProp(b, ",unique", c.Unique) writeTagProp(b, ",conflict=%s", c.Conflict) - writeTagProp(b, ",username", c.Username) - writeTagProp(b, ",password", c.Password) + if strict { + writeTagProp(b, ",username", c.Username) + writeTagProp(b, ",password", c.Password) + } if tb := c.ReferenceTableName; tb != "" { // write the ref line. @@ -169,8 +171,8 @@ func (c *Column) FieldTagString(strict bool) string { writeTagProp(b, ",unique_index=%s", c.UniqueIndex) writeTagProp(b, ",check=%s", c.CheckConstraint) - writeTagProp(b, ",auto", c.AutoGenerated) if strict { + writeTagProp(b, ",auto", c.AutoGenerated) writeTagProp(b, ",presenter", c.Presenter) writeTagProp(b, ",unscannable", c.Unscannable) } diff --git a/desc/struct_table.go b/desc/struct_table.go index e2dd29e..8dc7c3f 100644 --- a/desc/struct_table.go +++ b/desc/struct_table.go @@ -254,6 +254,7 @@ func convertStructFieldToColumnDefinion(tableName string, field reflect.StructFi if c.PrimaryKey && !c.Nullable && c.Type == UUID && c.Default == "" { c.Default = genRandomUUIDPGCryptoFunction1 + // c.AutoGenerated = true } if c.Password && c.Type == InvalidDataType { diff --git a/desc/update_query.go b/desc/update_query.go index ed57d0b..d9cc793 100644 --- a/desc/update_query.go +++ b/desc/update_query.go @@ -14,8 +14,20 @@ func BuildUpdateQuery(value any, columnsToUpdate []string, primaryKey *Column) ( return "", nil, err } + shouldUpdateID := false + for _, col := range columnsToUpdate { + if col == primaryKey.Name { + shouldUpdateID = true + break + } + } + + if len(args) == 1 { // the last one is the id. + return "", nil, fmt.Errorf("no arguments found for update, maybe missing struct field tag of \"%s\"", DefaultTag) + } + // build the SQL query using the table definition and its primary key. - query := buildUpdateQuery(primaryKey.Table, args, primaryKey.Name) + query := buildUpdateQuery(primaryKey.Table, args, primaryKey.Name, shouldUpdateID) return query, args.Values(), nil } @@ -61,7 +73,7 @@ func extractUpdateArguments(value any, columnsToUpdate []string, primaryKey *Col return args, nil } -func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string) string { +func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string, shouldUpdateID bool) string { var b strings.Builder b.WriteString(`UPDATE "` + td.Name + `" SET `) @@ -71,6 +83,12 @@ func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string) string { for i, a := range args { c := a.Column + if !shouldUpdateID && c.Name == primaryKeyName { + // Do not update ID if not specifically asked to. + // Fixes #1. + continue + } + if i > 0 { b.WriteByte(',') }