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

unique #76

Closed
ditiem opened this issue Jul 29, 2018 · 4 comments
Closed

unique #76

ditiem opened this issue Jul 29, 2018 · 4 comments

Comments

@ditiem
Copy link

@ditiem ditiem commented Jul 29, 2018

I have have a table, HotelRoom that has a name (string) and a hotel_id (number). I want to create a "unique index" to be sure that those couple of values are not repeated in the table.

Typically in SQL this is a UNIQUE INDEX on "table_name" ( field1, field2 )

Maybe something like:

nSQL( 'hotel_room' ).model( ... ).unique( [ [ 'hotel_id', 'hotel_room_name' ] ] )

where unique get a list of unique indexes. For the case of [ [ 'hotel_id' ] ] is the same as setting "idx" in the props of "hotel_id".

How do you guys see it?

@only-cliches
Copy link
Owner

@only-cliches only-cliches commented Aug 7, 2018

This feature is live as of 1.7.5. You simply add the unique value in your props, like this:

nSQL("hotels")
.model([
    {key: "id", type: "int", props: ["pk", "ai"]},
    {key: "hotel_room_name", type: "string", props: ["unique"]}
]);

The unique prop causes a secondary index to be generated on that column automatically, so doing something like ["unique", "idx"] would be redundant.

Let me know if this works as you expected.

@ditiem
Copy link
Author

@ditiem ditiem commented Aug 7, 2018

Thank you Scott!

I have a doubt/question. I understand that "unique" makes that value in the table unique, but what about if a pair is "unique". That means if a pair like (hotel_id, hotel_room_id) is unique, then (1,1) and (1,2) can exists (notice hotel_id alone is not "unique"), but adding another (1,1) will complain.

The "unique" option in props is great! I was using "idx" but I was not sure if that assures elements could not be repeated.

Thanks a lot!

@only-cliches
Copy link
Owner

@only-cliches only-cliches commented Aug 7, 2018

Hello David,

So first the idx prop does not guarantee uniqueness, multiple rows can easily have the same value.

For the use case you're talking about, I would create a specific column to store the combined values and make that unique, then use a row filter to automatically generate keys when data is inserted, like this:

nSQL("hotels")
.model([
    {key: "id", type: "int", props: ["pk", "ai"]},
    {key: "hotel_room_id", type: "int"}
    {key: "hotel_room_name", type: "string"},
    {key: "hotel_room_key", type: "string", props: ["unique"]}
])
.rowFilter(row => ({
    ...row,
    hotel_room_key: row.hotel_room_id + "-" + row.hotel_room_name
}));

Let me know if that works for what you're trying to achieve.

@ditiem
Copy link
Author

@ditiem ditiem commented Aug 7, 2018

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

Successfully merging a pull request may close this issue.

None yet
2 participants