Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please allow to specify two or more primary key columns (composite primary key) #114

Open
TheSQLWhisperer opened this issue Mar 5, 2017 · 2 comments
Labels

Comments

@TheSQLWhisperer
Copy link

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.

@AlekSi
Copy link
Member

AlekSi commented Mar 6, 2017

That's correct that reform assumes single-column primary key for Records, 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)

@TheSQLWhisperer
Copy link
Author

TheSQLWhisperer commented Mar 6, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants