-
Notifications
You must be signed in to change notification settings - Fork 114
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
Enable custom 'key' field #238
Comments
Can you explain what use-case this might help? Mongo also has fixed "id" field name |
I am not using Mongo but a SQL database, so I am not linked to that limitation. I a managing a set of objects that comes from an external entity that do not have 'id', but a 'name' field a uniq key. Right now I am using a dirty middleware to translate one schema to another (by renaming this specific field), but this is a solution I don't like at all. |
Personally I feel it's a reasonable limitation for The MongoDB back-end renames queries against s := sqlstore.New(db *sql.DB)
s.SetIDColumn("id")
idx.Bind(...., s) |
I am with @smyrman. Having |
Okay, I can agree that there it may be good to have an specific field that refers to the key with consistency on its name. Do you think it's possible to make this ID to be a clone of another field easily? Let's say that I want this 'id' field to be a clone of the 'name' field. How bad do you see that possible solution? |
I gave example for cloning for GET in my comment above. Do you need PUT/POST as well? |
Yes, I need it for POST specifically. This a non-working-code-yet that I am going to experiment with. Maybe it's an stupid solution, so comments are welcome :)
But I am getting this "2019/03/01 19:13:58 Server error: Missing ID field", because NewItem() checks the existence of the id field :( |
Can you specify how you are initializing this |
I do
And it works when I pass an empty "id" field, but not when it's not there. |
First of all, if you are able to modify the database to have a separate ID column, either a unique ID (e.g. xid) or a unique random ID (e.g. UUID v4), that would be the preferred solution in my opinion. This ID would be generated by the back-end on Create. Sometimes this isn't possible though, e.g. if the database is defined/designed outside your application. There are at least two options you can try for allowing a blank
For 2, there are lots of pitfalls to be aware of, as in running Prepare/Validate is your responsibility. However you are able to modify and prepare the pipeline as you see fit. A good start would be looking at the code for the We have a lot of cases where we do 2 for internal communication or complex tasks involving several resources (possibly external / non-rest-alyer ones), but the we do it via JSON RPC 2.0, and don't do anything REST-like. |
@apuigsech You are seeing this error, because |
The problem is that I am not able to change the contract, because it is already stablished and I don't have any control of the client-side, so I have to adapt to the server to it. The contract implements a very simple REST that using REST-layer can be developed quickly and in an elegant way. The only requirement is having a different key field.
This is exactly what I tried; Having a non-required id field, so the request is procesable, and then use Hooks to adapt it, so it's transparent for REST-layer. The problem is that there isn't a Hook executed before NewItem() is executed, and it has this code;
initially I don't like the idea as this affects to most of the endpoints the REST-layer creates, so at the end it's like do not use it, but I will evaluate this option more in depth. Thank you so much for the ideas! |
Yes, a Middleware is the only viable option I see right now. Thanks! |
By the way, do you think that adding support to REST-layer for custom key field would be interesting? Something like;
|
Hello guys, I am very interested in this question. Currently, I think I have a use case where it would be natural not to have a hardcoded 'id' as primary key. Due to a many to many relationship which forms associative entity in database, I want to use a composite primary key (made from foreigh keys) as it's primary key. I am aware that I could solve my issue by having 'id' field as primary key and composite unique key for these foreign keys I have, although the 'id' field is logically redundant. Since I am not so experienced, I would be grateful if you could share practical benefits of this practice with me. Besides the naming consistency and easier referencing in more complicated relationships (e.g. associative entity related to another entity forming another associative entity) are there any more advantages of the mandatory 'id' field? Thank you for your time! |
The field used as key have to be 'id' right now. Would be great to be able to change this specific behavior and use a different field name as key.
The text was updated successfully, but these errors were encountered: