uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.64k stars 220 forks source link

Fix M2M relations for models with composite keys #996

Open ygabuev opened 4 months ago

ygabuev commented 4 months ago

This PR adds support for M2M relations with composite keys. This feature was probably supposed to be working previously, but was not tested and turned out to be buggy. Existing functionality is not affected, according to the tests.

Commits:

Closes https://github.com/uptrace/bun/issues/992.

ygabuev commented 4 months ago

The new testCompositeM2M test fails for the mssql2019 target - it's the only failing test. The issue is that Microsoft SQL Server dialect does not support the (a, b) in ((v1, v2), ...) syntax for lists of tuple-like objects. See the discussion in this Stack Overflow question. This syntax is used to construct the M2M query at the moment.

Since this feature is non-breaking and only fails for a single, arguably niche, target, I'd be happy to see it merged as is. For MS SQL Server backends, we can add a runtime error during the db.RegisterModel call. Alternatively, we can investigate amending the syntax used for the M2M query generation, e.g. along these lines, but this would result in lengthier queries and thus negatively affects all the other SQL backends.

@vmihailenco what are your thoughts?