Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Unique Key on 2 Cols


Go to End


3 Posts   2038 Views

Avatar
rob.s

Community Member, 78 Posts

28 May 2010 at 12:50am

Edited: 28/05/2010 12:52am

Hi,

what is the right syntax to add a 2-col unique index ?
I'd like to have a unique index on Field_1 AND Field_2

    static $indexes = array(
            "Uniques" => ????,
    );

in SQL would be:

...
UNIQUE KEY `myUniqueIndex` (`FIELD_1`,`FIELD_2`),
...

Avatar
rob.s

Community Member, 78 Posts

10 June 2010 at 2:24am

Edited: 10/06/2010 2:24am

no idea ?

(sry for bump)

Avatar
mark_s

Community Member, 78 Posts

11 June 2010 at 12:00pm

Edited: 11/06/2010 12:05pm

Hi.

You should be able to do this:

<code php>
static $indexes = array(
"Uniques" => array(
"type" => "unique",
"value" => "`FIELD_1`,`FIELD_2`"
)
);
</code>

Another variation is:

<code php>
static $indexes = array(
"Uniques" => "(`FIELD_1`,`FIELD_2`)",
);
</code>

But this doesn't create a unique index, just a two-column index called Uniques.

Mark