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

upsert not works as expect if exist update then insert #224

Open
tablecell opened this issue Mar 2, 2017 · 1 comment
Open

upsert not works as expect if exist update then insert #224

tablecell opened this issue Mar 2, 2017 · 1 comment

Comments

@tablecell
Copy link

tablecell commented Mar 2, 2017

field uid without unique index

CREATE TABLE contact (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(255) NOT NULL,
address varchar(255) COLLATE utf8_unicode_ci DEFAULT '')
ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

 $mapper = $spot->mapper('Entity\Contact');
  //$mapper->migrate();
   $mapper->upsert(['address'=>'xxx'],['uid'=>14]);

nothing happened

 $mapper->upsert(['uid'=>14','address'=>'xxx'],['uid'=>14]);

add new row with (id=5,uid=14, address=xxx)

repeat

 $mapper->upsert(['uid'=>14','address'=>'xxx'],['uid'=>14]);

add new row with (id=6,uid=14, address=xxx)

  $mapper->upsert(['address'=>'yyyy'],['uid'=>14]);

(id=5,uid=14, address=xxx) changed to (id=5,uid=14, address=yyyy )


field uid with unique index
CREATE TABLE contact (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(255) NOT NULL,
address varchar(255) COLLATE utf8_unicode_ci DEFAULT ,
UNIQUE KEY uid (uid)

)
ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

   $mapper->upsert(['address'=>'aaaa'],['uid'=>24]);

nothing happened

  $mapper->upsert(['uid'=>24,'address'=>'aaaa'],['uid'=>24]);

add new row (id=7,uid=24,address=aaaa)

repeat

  $mapper->upsert(['uid'=>24,'address'=>'aaaa'],['uid'=>24]);

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '24' for key 'uid'' in

  $mapper->upsert([ 'address'=>'bbbb'],['uid'=>24]);

(id=7,uid=14, address=aaaa) changed to (id=7,uid=24, address=bbbb )

conclusion:
if first param $data include uid then insert new record
first param $data exclude uid then update record

not automatic as expect

@willemwollebrants
Copy link
Contributor

Check your entity definition. You probably forgot to mark 'uid' as a unique field there:

class Contact extends Spot\Entity
{
    protected static $table = 'contact';

    public static function fields()
    {
        return [
            'id' => ['type' => 'integer', 'primary' => true, 'autoincrement' => true],
            'uid' => ['type' => 'integer', 'default' => 0, 'unique' => true],
            'address' => ['type' => 'text']
        ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants