reform
reform copied to clipboard
Please allow to specify two or more primary key columns (composite primary key)
I have a table with two foreign keys (id1 and id2), there are no other columns in the table. The primary key on this table is a composite key (id1,id2). The current version of go-reform does not allow to specify such a primary key. This is a common case and there is no way to use save on such a schema.
In general go-reform always assumes that there is only one primary key column and it's a serial type. It's not even possible to preset it to some value and save the record (the library generates an UPDATE in this case) This is a strange requirement for a generic library.
That's correct that reform assumes single-column primary key for Record
s, it is mentioned in README. In fact, one of the proprietary reincarnations supported multi-column PKs, but they caused too many problems and subtle errors, so they never made their way to open-source version.
But I'm not sure why you think it has to be of a serial type. That's true that database/sql
supports only int64
PK with LastInsertId()
. But you can use any type with RDBMS which supports alternative mechanism of reporting autogenerated identifier (i.e. PostgreSQL with INSERT … RETURNING
syntax). More than that, you can set some value and use Insert()
to insert a Struct
– it's not even a Record
, so this field don't have to be (but can be) a PK. If you use Save()
, then yes, reform will generate an UPDATE
– but if this UPDATE
changes zero rows, in will generate INSERT
. That's all documented and covered with tests.
As for the first part of the issue. While it's not possible to generate a Record
for such type, you can generate a Struct
if you omit pk
tag value for both fields. It fill not allow you to use Update()
, Save()
and Delete()
, but your use case still should be covered by Insert()
and DeleteFrom()
.
I guess what you really want is some form of many-to-many relationship support. That may come after we investigate and implement support for one-to-many (#54).
(edited: formatting)
I don't want many to many support in the ORM.
I would like to have composite keys (possibly with some manual extension not to slow down the common path) and I would like someway to generate INSERT directly from Save when I know the key, instead of an UPDATE checking for 0 rows (this is a very wasteful round trip). Possibly this might require some flag to be set manually by the user to indicate that this row is a new row (and INSERT should be called instead of UPDATE).
Using Insert worked for me but IMHO having two interfaces that essentially do the same thing is confusing.
I think overall the library is nicely made and could be useful.