diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index f5785e491..b4da8920f 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -1001,6 +1001,14 @@ func TestQuery(t *testing.T) { func(db *bun.DB) schema.QueryAppender { return db.NewUpdate().Model(&Model{42, ""}).OmitZero() }, + func(db *bun.DB) schema.QueryAppender { + q := db.NewCreateTable().Model(new(Story)).WithForeignKeys() + + // Check that building the query with .AppendQuery() multiple times does not add redundant FK constraints: + // https://github.com/uptrace/bun/pull/941#discussion_r1443647857 + _ = q.String() + return q + }, } timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-163 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-163 new file mode 100644 index 000000000..8df726dcb --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-163 @@ -0,0 +1 @@ +CREATE TABLE `stories` (`id` BIGINT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), `user_id` BIGINT, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-163 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-163 new file mode 100644 index 000000000..967afa9ca --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-163 @@ -0,0 +1 @@ +CREATE TABLE "stories" ("id" BIGINT NOT NULL IDENTITY, "name" VARCHAR(255), "user_id" BIGINT, PRIMARY KEY ("id"), FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-163 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-163 new file mode 100644 index 000000000..8df726dcb --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-163 @@ -0,0 +1 @@ +CREATE TABLE `stories` (`id` BIGINT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), `user_id` BIGINT, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-163 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-163 new file mode 100644 index 000000000..8df726dcb --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-163 @@ -0,0 +1 @@ +CREATE TABLE `stories` (`id` BIGINT NOT NULL AUTO_INCREMENT, `name` VARCHAR(255), `user_id` BIGINT, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-163 b/internal/dbtest/testdata/snapshots/TestQuery-pg-163 new file mode 100644 index 000000000..ccb26f415 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-163 @@ -0,0 +1 @@ +CREATE TABLE "stories" ("id" BIGSERIAL NOT NULL, "name" VARCHAR, "user_id" BIGINT, PRIMARY KEY ("id"), FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-163 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-163 new file mode 100644 index 000000000..ccb26f415 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-163 @@ -0,0 +1 @@ +CREATE TABLE "stories" ("id" BIGSERIAL NOT NULL, "name" VARCHAR, "user_id" BIGINT, PRIMARY KEY ("id"), FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-163 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-163 new file mode 100644 index 000000000..5e4baec58 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-163 @@ -0,0 +1 @@ +CREATE TABLE "stories" ("id" INTEGER NOT NULL, "name" VARCHAR, "user_id" INTEGER, PRIMARY KEY ("id"), FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION) diff --git a/query_table_create.go b/query_table_create.go index eec523673..105f26c0b 100644 --- a/query_table_create.go +++ b/query_table_create.go @@ -372,3 +372,12 @@ func (q *CreateTableQuery) afterCreateTableHook(ctx context.Context) error { } return nil } + +func (q *CreateTableQuery) String() string { + buf, err := q.AppendQuery(q.db.Formatter(), nil) + if err != nil { + panic(err) + } + + return string(buf) +}