go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.19k stars 3.88k forks source link

With Postgres, fails to create records with belongs_to association #5219

Open pioz opened 2 years ago

pioz commented 2 years ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/458

Description

A query fails to create (in this example articles) because the set_id column (foreign key) is set to 0 and breaks the constraint. This happened, I think, because the previous query that inserts the sets returns not only the id, but also the column parent_code: ON CONFLICT DO NOTHING RETURNING "parent_code","id"

On MySQL the test passed.

This is strange behavior for an ORM library that which the same code works with a driver (MySQL) and fails with another (Postgresql).

pioz commented 2 years ago

This seems caused because in the field parent_code I have set the tag gorm:"default:(-)", that I cannot remove. And the fields with this kind of tag are returned in the RETURNING statement.