C# SQLite-net define multi column unique
Asked Answered
B

2

21

I have seen references to changes in SQLite-net that supports multi-column unique constraints. I know it can be done directly with sqlite however I prefer to stay wit the sqlite-net methods of doing things. What is the Syntax to do multi-column unique. Single is [Unique] above the column desired to be unique.

Becalm answered 11/9, 2013 at 22:37 Comment(0)
B
50

I have found the answer by reviewing the actual unit tests included in the project. It is base upon using the named parameters on the index attribute. For example:

    [Indexed(Name = "ListingID", Order = 2, Unique = true)]
    public string   ListingNumber { get; set; }
    [Indexed(Name = "ListingID", Order = 1, Unique = true)]
    public string   ChannelCode { get; set; }

will create an index named ListingID over two fields that must be unique. It you do not want the unique attribute, remove it as a parameter. You must use the named parameters to make it work. Also all field in an index must have the same Unique value.

Becalm answered 12/9, 2013 at 6:52 Comment(2)
Thanks! I was just looking for this. What is the syntax for having multiple indices on the same field (a compound index like yours, and another index on same field but by itself)? Everything I try is getting a duplicate index error from the compiler.Mchail
@RobertOschler Found this issue on github and from there got the workaround for doing multiple indices on the same field. Are you roschler?Sfumato
H
0

Here's another solution

public string   ListingNumber { get; set; }
public string   ChannelCode { get; set; }
[Unique] 
public string UniqueChannelCodeListingNumber => $"{ListingNumber}_{ChannelCode}";
Huehuebner answered 25/6, 2024 at 10:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.