-
-
Notifications
You must be signed in to change notification settings - Fork 156
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
Do not assume that the primary key is "id" #6
Comments
You can retrieve the primary key of a
|
Hello @wstucco First step is to support single-field custom primary keys. Later, I'll see if supporting composite primary keys is feasible or not. |
+1 this, would be great for those of us incorporating phoenix on top of legacy databases |
Hi @aesmail ! I have have been using Kaffy for a small project that has some joiner tables, where I left off the id and used a composite primary key instead. As Kaffy doesn't currently support either, I thought it was a good opportunity to look into it a bit and see if I could come up with a working solution. It's not done yet, but it's at a point where basic navigation and modification seem fine, and it would be great to get some feedback, to see if it's worth moving forward with. The approach used is to introduce The major places where a hard coded Would love to hear your thoughts on this. |
Hey @nullpilot, I'll keep your PR open for now, but I expect that it will be extremely valuable in the coming days. |
Can I suggest to not to use the dot as a separator? In PostgreSQL, for example, this code is valid CREATE TABLE test (
"id.id" INT
);
INSERT INTO test ("id.id") VALUES (1); I think using Json or Erlang ETF as serialization format would be better. Something along the lines of [<primary keys>]
|> :erlang.term_to_binary()
|> Base.encode64() or Jason.encode([<primary keys>]) |
@aesmail: Thanks for checking it out, glad to hear it might be helpful. I'll keep an eye on the repo for what you have coming up. @wstucco: The serialization as is only joins together the values, not the names of the keys, so in the case of uint/serial/uuid there shouldn't be a problem. In the case of strings or some other type that could introduce ambiguity, the methods could be overwritten to use one of the methods you suggest with a final In my local example the route currently ends up looking something like this for a binary-id composite:
|
Sorry, my fault, I was only trying to show that the dot can appear in many places one wouldn't think to find them. I think safer defaults will avoid weird bugs in the future. In the case of composite keys there's a very high chance they won't be of a safe base type. The problem is that if the keys have unsafe values the serializer will produce the wrong result, but the deserializer won't fail, it will simply emit the wrong key values. (1)> Enum.join([1.2, 3.4], ".")
"1.2.3.4"
(2)> String.split("1.2.3.4", ".") |> Enum.zip(["id", "sub_id"])
[{"1", "id"}, {"2", "sub_id"}] while ETF returns the correct result (3)> :erlang.term_to_binary([1.2, 3.4])
|> Base.encode64()
|> Base.decode64!()
|> :erlang.binary_to_term()
|> Enum.zip(["id", "sub_id"])
[{1.2, "id"}, {3.4, "sub_id"}] |
That's a good point, I like the solution as well. I do think there is some value in "readable" URLs that contain the actual IDs where possible (maybe even non-primary, unique slugs in the future, for example) - but having a more encompassing default is absolutely worth the consideration. Since the setup is quite flexible, it could even be combined to use a "best effort" approach, depending on the types of primary keys present in a schema. |
Me too! Maybe replacing the dot with a less common character (for example ^, ~, !) could reduce the risk of a collision to a minimum |
That sounds viable. There aren't many options: For now, let's see what @aesmail has in store for us 😃 |
@nullpilot @wstucco interesting discussion. So you guys have some perspective, here's the gist of what's coming: This way, Kaffy could potentially work with any data source (RDBMs, APIs, NoSQL, csv files, etc). It's trickier than I thought, but we're getting there slowly. Let me know if you guys have any feedback or comments. |
That sounds ambitious, but would be something with many use cases, beyond Kaffy possibly. Nothing specific comes to mind to comment on for now, but I can see how this issue among others gets some extra depth with this idea. Once it's at a point where it can be tested I'll give it a go! Thanks for sharing. |
@aesmail Once you have a branch for that, I can get started on setting up the |
FWIW We are using ReactAdmin, which doesn't have support for composite IDs. To circumvent this limitation we use a single param containing the composite ID serialized to JSON and url encoded using base64. For example:
|
Hi all, Where are we with using uuid / binary_id as a primary key? Is that supported? Thanks. |
Need a way to figure out the primary key.
Might also need to check if the primary key is a set of fields and not just a single field.
The text was updated successfully, but these errors were encountered: