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

Comments

Projects
None yet
2 participants
@ditiem
Copy link

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?

@ClickSimply

This comment has been minimized.

Copy link
Owner

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.

@ClickSimply ClickSimply closed this Aug 7, 2018

@ditiem

This comment has been minimized.

Copy link
Author

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!

@ClickSimply

This comment has been minimized.

Copy link
Owner

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

This comment has been minimized.

Copy link
Author

commented Aug 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.