GORM Playground Link

https://github.com/go-gorm/playground/pull/623 (tested with GORM_DIALECT=postgres)

Description

Explain your user case and expected results

Here is a many2many table relationship between 2 entities foo & bar:

foo_id bar_id
0 0
0 1
1 0
1 1
1 2

When I fetch the Foo entity & Preload all its Bar, the relationship is broken when foo_id=0 or bar_id=0

expected: - foo 0 has 2 bars - foo 1 has 3 bars

actual result: - foo 0 has 0 bar - foo 1 has 2 bars

--- FAIL: TestGORM (0.02s)
    main_test.go:36: Failed for FooID=0, expected 2 bars, but got 0
    main_test.go:36: Failed for FooID=1, expected 3 bars, but got 2

Comment From: MattKetmo

Note that using something else than a int or uint (which default value is not 0) can be a work around (waiting for proper fix)

For instance a decimal.Decimal or even a pointer *int / *uint

Comment From: saeidee

Zero is not a common ID that should be used as a foreign key, it often denotes an empty field / NULL value.

Comment From: MattKetmo

Yes I know 0 is not a "common" ID but in my case this is the reference of an item outside my app so it makes sense I import as is. To denote an empty value I would have used a NULLable column, which is not the case here.

Joining using 0 as id is not a db issue, only an gorm issue.

For now I got around it using a decimal.Decimal instead of uint.

Comment From: MattKetmo

ok so no proper fix possible except using a workaround by not using integers?

Comment From: saeidee

Yes I know 0 is not a "common" ID but in my case this is the reference of an item outside my app so it makes sense I import as is.

This is specific to your application's business logic, however, feel free to open a fix PR.