Skip to content

Commit d347b48

Browse files
committed
feat(dialect): return default on update/delete when create table
Close #1212
1 parent b25423b commit d347b48

File tree

7 files changed

+34
-17
lines changed

7 files changed

+34
-17
lines changed

dialect/feature/feature.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
DeleteOrderLimit // DELETE ... ORDER BY ... LIMIT ...
4242
DeleteReturning
4343
AlterColumnExists // ADD/DROP COLUMN IF NOT EXISTS/IF EXISTS
44+
FKDefaultOnAction // FK ON UPDATE/ON DELETE has default value: NO ACTION
4445
)
4546

4647
type NotSupportError struct {
@@ -91,4 +92,5 @@ var flag2str = map[Feature]string{
9192
DeleteOrderLimit: "DeleteOrderLimit",
9293
DeleteReturning: "DeleteReturning",
9394
AlterColumnExists: "AlterColumnExists",
95+
FKDefaultOnAction: "FKDefaultOnAction",
9496
}

dialect/mssqldialect/dialect.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func New(opts ...DialectOption) *Dialect {
4949
feature.Identity |
5050
feature.Output |
5151
feature.OffsetFetch |
52+
feature.FKDefaultOnAction |
5253
feature.UpdateFromTable |
5354
feature.MSSavepoint
5455

@@ -158,15 +159,15 @@ func (d *Dialect) AppendString(b []byte, s string) []byte {
158159
return d.BaseDialect.AppendString(b, s)
159160
}
160161

161-
func (d *Dialect) DefaultVarcharLen() int {
162-
return 255
163-
}
164-
165162
func (d *Dialect) AppendSequence(b []byte, _ *schema.Table, _ *schema.Field) []byte {
166163
return append(b, " IDENTITY"...)
167164
}
168165

169-
func (d *Dialect) DefaultSchema() string {
166+
func (*Dialect) DefaultVarcharLen() int {
167+
return 255
168+
}
169+
170+
func (*Dialect) DefaultSchema() string {
170171
return "dbo"
171172
}
172173

dialect/mysqldialect/dialect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func New(opts ...DialectOption) *Dialect {
4848
feature.InsertOnDuplicateKey |
4949
feature.SelectExists |
5050
feature.CompositeIn |
51+
feature.FKDefaultOnAction |
5152
feature.UpdateOrderLimit |
5253
feature.DeleteOrderLimit
5354

dialect/pgdialect/dialect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func New(opts ...DialectOption) *Dialect {
5555
feature.SelectExists |
5656
feature.GeneratedIdentity |
5757
feature.CompositeIn |
58+
feature.FKDefaultOnAction |
5859
feature.DeleteReturning |
5960
feature.AlterColumnExists
6061

dialect/sqlitedialect/dialect.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func New(opts ...DialectOption) *Dialect {
4141
feature.SelectExists |
4242
feature.AutoIncrement |
4343
feature.CompositeIn |
44+
feature.FKDefaultOnAction |
4445
feature.DeleteReturning
4546

4647
for _, opt := range opts {
@@ -102,7 +103,7 @@ func (d *Dialect) AppendBytes(b []byte, bs []byte) []byte {
102103
return b
103104
}
104105

105-
func (d *Dialect) DefaultVarcharLen() int {
106+
func (*Dialect) DefaultVarcharLen() int {
106107
return 0
107108
}
108109

@@ -132,7 +133,7 @@ func (d *Dialect) AppendSequence(b []byte, table *schema.Table, field *schema.Fi
132133
// DefaultSchemaName is the "schema-name" of the main database.
133134
// The details might differ from other dialects, but for all means and purposes
134135
// "main" is the default schema in an SQLite database.
135-
func (d *Dialect) DefaultSchema() string {
136+
func (*Dialect) DefaultSchema() string {
136137
return "main"
137138
}
138139

query_table_create.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,23 @@ func (q *CreateTableQuery) appendFKConstraintsRel(fmter schema.Formatter, b []by
318318

319319
for _, key := range keys {
320320
if rel := relations[key]; rel.References() {
321+
query := "(?) REFERENCES ? (?)"
322+
args := []any{
323+
Safe(appendColumns(nil, "", rel.BasePKs)),
324+
rel.JoinTable.SQLName,
325+
Safe(appendColumns(nil, "", rel.JoinPKs)),
326+
}
327+
if len(rel.OnUpdate) > 0 {
328+
query += " ?"
329+
args = append(args, Safe(rel.OnUpdate))
330+
}
331+
if len(rel.OnDelete) > 0 {
332+
query += " ?"
333+
args = append(args, Safe(rel.OnDelete))
334+
}
321335
b, err = q.appendFK(fmter, b, schema.QueryWithArgs{
322-
Query: "(?) REFERENCES ? (?) ? ?",
323-
Args: []interface{}{
324-
Safe(appendColumns(nil, "", rel.BasePKs)),
325-
rel.JoinTable.SQLName,
326-
Safe(appendColumns(nil, "", rel.JoinPKs)),
327-
Safe(rel.OnUpdate),
328-
Safe(rel.OnDelete),
329-
},
336+
Query: query,
337+
Args: args,
330338
})
331339
if err != nil {
332340
return nil, err

schema/table.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/jinzhu/inflection"
1313

14+
"github.com/uptrace/bun/dialect/feature"
1415
"github.com/uptrace/bun/internal"
1516
"github.com/uptrace/bun/internal/tagparser"
1617
)
@@ -623,7 +624,10 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
623624
rel.Condition = field.Tag.Options["join_on"]
624625
}
625626

626-
rel.OnUpdate = "ON UPDATE NO ACTION"
627+
if t.dialect.Features().Has(feature.FKDefaultOnAction) {
628+
rel.OnUpdate = "ON UPDATE NO ACTION"
629+
rel.OnDelete = "ON DELETE NO ACTION"
630+
}
627631
if onUpdate, ok := field.Tag.Options["on_update"]; ok {
628632
if len(onUpdate) > 1 {
629633
panic(fmt.Errorf("bun: %s belongs-to %s: on_update option must be a single field", t.TypeName, field.GoName))
@@ -638,7 +642,6 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
638642
rel.OnUpdate = s
639643
}
640644

641-
rel.OnDelete = "ON DELETE NO ACTION"
642645
if onDelete, ok := field.Tag.Options["on_delete"]; ok {
643646
if len(onDelete) > 1 {
644647
panic(fmt.Errorf("bun: %s belongs-to %s: on_delete option must be a single field", t.TypeName, field.GoName))

0 commit comments

Comments
 (0)