Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Unique Key on 2 Cols

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

Page: 1
Go to End
Author Topic: 1423 Views
  • rob.s
    Avatar
    Community Member
    78 Posts

    Unique Key on 2 Cols Link to this post

    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`),
    ...

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Unique Key on 2 Cols Link to this post

    no idea ?

    (sry for bump)

  • mark_s
    Avatar
    Community Member
    78 Posts

    Re: Unique Key on 2 Cols Link to this post

    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

    1423 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.