The Man The Man - 2 years ago 98
SQL Question

Silverstripe duplicate entries even with unique index

I'm trying to prevent duplicate records when adding customer records in my crm with the following index,

private static $indexes = array(
'IndexFirstSurName' => array(
'type' => 'unique',
'value' => '"FirstName","Surname"'

note that I extended Customer from Member were FirstName and Surname came from

class Customer extends Member

But Silverstripe is still allowing duplicate entries of FirstNane and Surname combination? Anyone experienced the same?

Answer Source

The Man, in my experience a validate() is still needed even when using indexing:

public function validate() {
    $result = parent::validate();

    if(Member::get()->filter(array('FirstName' => $this->FirstName, 'Surname' => $this->Surname))->first()) {
        $result->error('First and Surname must be unique for each member.');

    return $result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download