Duplicate key error during upsert [Explanation] - php

I am executing the below statement in a class. This code is from
$query = array('_id' => $id, 'lock' => 0);
$update = array('$set' => array('lock' => 1));
$options = array('safe' => true, 'upsert' => true);
$result = $this->_mongo->update($query, $update, $options);
if ($result['ok'] == 1) {
return true;
}
However I do not understand how I would get a duplicate key error.
Can someone explain the possible scenarios and likelihood that I will receive this error?
I have been researching this extensively, cannot find my answer anywhere. So if it is on SO or any other website please share!
Thanks in advance.

Since you're doing an upsert and including _id in your query, you shouldn't be getting any duplicates on that key. This makes me think that you've created a unique index on lock, which isn't going to work for more than 2 documents because you only have 2 values for that field.
If you haven't put a unique index on lock, then you must have a unique index on a field you aren't showing here. That won't work either because on an insert, your upsert is going to set _id and lock only, any other field with an index will be inserted as null. If one of those fields has a unique index, then only a single document can have a null in that field. So when you try and insert another null for that field, you'll get a duplicate key error.

Related

I am trying to update but it is adding a new row

this is my code for updating:
PS: empid is a foreign key but i think that shouldnt be the reason and the code is in CakePHP
if($this->request->is('post'))
{
$this->request->data["Leave"]["empid"] = $this->request->data["id"];
$this->Leave->empid = $this->request->data["Leave"]["empid"];
$this->request->data["Leave"]["leave_start"] = $this->request->data["start_date"];
$this->request->data["Leave"]["leave_end"] = $this->request->data["end_date"];
$this->request->data["Leave"]["leave_taken"] = $this->request->data["leave_taken"];
if($this->Leave->save($this->request->data['Leave']))
{
return $this->redirect(array('action' => 'manage_leave'));
}
}
// This code is inserting a new row instead of updating and also not adding any value in the new row
May be your trying to update the foreign table data using simple save.
Update multiple records for foreign key
Model::updateAll(array $fields, mixed $conditions)
Example
$this->Ticket->updateAll(
array('Ticket.status' => "'closed'"),
array('Ticket.customer_id' => 453)
);
Simple save for the primary key
Make sure that your HTML has empid
echo $this->Form->input('Leave.empid', array('type' => 'hidden'));
Save Model
$this->Leave->empid = $this->request->data["Leave"]["empid"]; //2
$this->Leave->save($this->request->data);
In between, you can also try to set the model data and check the $this->Leave->validates() and $this->Leave->validationError if they are giving any validation errors.
// Create: id isn't set or is null
$this->Recipe->create();
$this->Recipe->save($this->request->data);
// Update: id is set to a numerical value
$this->Recipe->id = 2;
$this->Recipe->save($this->request->data);
You can find more information about all Saving your data
Hope this helps you :)
And in case if $empid is primary key of corresponding table of Leave model (e.g leaves), Just replace:
$this->Leave->empid = $this->request->data["Leave"]["empid"];
By
$this->Leave->id = $this->request->data["Leave"]["empid"];

Codeigniter 3 update mysql not working

I am having an issue with updating a MySql table using codeigniter.
Basically, its inserting 'img' the characters, into the table rather than the value of the variable.
This is so strange!
Here is my model:
public function update_course_progress($progress_data) {
$course_id = $progress_data['course_id'];
$user_id = $progress_data['user_id'];
$progress = $progress_data['progress'];
$status = $progress_data['status'];
$update_data = array (
'progress' => $progress,
'status' => $status,
);
// perform update on the matching row
$this->db->update('training_stats', $update_data, array('course_id' => $course_id, 'user_id' => $user_id));
}
So, the issue is with 'progress' instead of inserting the value of this variable it is inserting 'img'???
So, if i var_dump $update_data i get this:
array(2) {
["progress"]=> string(2) "1a"
["status"]=> string(1) "i"
}
Which is correct:
And if i use the profiler in CI to get the db queries, this is what I get:
UPDATE `training_stats`
SET `progress` = '1a', `status` = 'i'
WHERE `course_id` = '8'
AND `user_id` = '2'
Which is correct.
So WHY ON EARTH is it inserting null into the db instead of 1a.
The table structure for this column is VARCHAR(4).
progress varchar(4) NOT NULL DEFAULT '0',
What the hell is going on? why on earth is it img input???
What can be wrong?
UPDATE:
As i was debugging, i tried an insert instead of an update, and 2 rows were inserted. The first row was the expected data, and the second row was the data with 'img' in it. Both rows were the same except for the 'progress' column, which had img inserted in the second row.
So obviously it had been updating the row with the correct data and then overwriting it with the incorrect data.
But now why are there 2 rows being inserted? There is no loop? and why is the CI profiler not logging the second query, if that is indeed what is happening
As of per documentation of CI I will use the more standard method of updating data.
$updateArray = array (
'progress' => $progress_data['progress'],
'status' => $progress_data['status'],
);
$whereArray = array(
'course_id' => $progress_data['course_id'],
'user_id' => $progress_data['user_id']
)
$this->db->set($updateArray);
$this->db->where($whereArray);
$this->db->update('training_stats');
This should do, I also think you shouldn't put extra variables for the data as you did. With such short function you really are not having any benefits sinds all data is only accessed once and seem like unnecessary to me, though opinions could vary.

Update a row using idiorm and php

I have this function to update a record, but i cannot it fails and send me a "Primary key ID missing from row or is null" message, how can I fix it?
public static function update_child($data)
{
try
{
$update= ORM::for_table("dm_child",DM_TAG)
->where_equal($data["id_child"]);
$update -> set(array(
"gender" => $data["gender"]
"age_year" =>$data["year"]
"age_month" => $data["month"]
));
$update -> save();
}
catch(PDOException $ex)
{
ORM::get_db()->rollBack();
throw $ex;
}
}
Idiorm assumes that the name of the primary key is 'id', which is not that, in your case.
Therefore you have to explicitly specify it to Idiorm:
<?php
ORM::configure('id_column_overrides', array(
'dm_child' => 'id_child',
'other_table' => 'id_table',
));
See Docs>Configuration.
The answer is indeed the one provided by #iNpwd for changing the default 'id' column name for queries on a per table basis:
ORM::configure('id_column_overrides', array(
'table_name' => 'column_name_used_as_id',
'other_table' => array('pk_1', 'pk_2') // a compound primary key
));
The thing that was biting me on getting it to recognize my query was WHERE I was changing the ORM::configure values. I was not in the correct file.
A deeper link to specifically the ID Column configuration: http://idiorm.readthedocs.org/en/latest/configuration.html#id-column
I just met this problem 2 minutes ago. The real reason is, you forgot select id field in querying.
demo:
$demo = ORM::for_table('demo')->select('field_test')->find_one($id);
$demo->field_test = 'do';
$demo->save();
You will get the error.
change to :
$demo = ORM::for_table('demo')->select('field_test')->select('id')->find_one($id);
It will fix the problem.
Some tips in documents:
https://github.com/j4mie/idiorm/blob/master/test/ORMTest.php
/**
* These next two tests are needed because if you have select()ed some fields,
* but not the primary key, then the primary key is not available for the
* update/delete query - see issue #203.
* We need to change the primary key here to something other than id
* becuase MockPDOStatement->fetch() always returns an id.
*/
I've never used idiorm, so cannot guarantee that my answer will work for you, but from this page and under "Updating records", we have an example which is similar but slightly different to yours.
// The 5 means the value of 5 in the primary-key column
$person = ORM::for_table('person')->find_one(5);
// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;
// This is equivalent to the above two assignments
$person->set(array(
'name' => 'Bob Smith',
'age' => 20
));
// Syncronise the object with the database
$person->save();
I'm sure I'll learn the reason behind this, but let me tell you all I understand at the moment, and how I "fixed" it.
Here is the beginning of idiorm's save function:
public function save() {
$query = array();
// remove any expression fields as they are already baked into the query
$values = array_values(array_diff_key($this->_dirty_fields, $this->_expr_fields));
if (!$this->_is_new) { // UPDATE
// If there are no dirty values, do nothing
if (empty($values) && empty($this->_expr_fields)) {
return true;
}
$query = $this->_build_update();
$id = $this->id(true);
Right there, on that last line, when trying to access the $this->id, you are getting an exception thrown:
throw new Exception('Primary key ID missing from row or is null');
$this does not contain an id property. I'm not really sure how it could. The example given both on their homepage and in the docs doesn't do anything special to address this. In fact I am copying them 1:1 and still yielding the same error as you.
So, all that said, I fixed this error by just adding in my own id:
$crop = ORM::for_table('SCS_Crop')->find_one($id);
$crop->id = $id;
$crop->Name = "Foo";
$crop->save();
This also happens when the id field name is ambiguous, e.g. when joining two tables both having an id column. This is the case with referenced tables
Model::factory('tableOne')
->left_outer_join('tableTwo', array('tableOne.tableTwo_id', '=', 'tableTwo.id'))
->find_one($id);
In these cases set an alias to the ID column of the parent tableOne to later access it while saving. Make sure that you also select other columns you need - e.g. by ->select('*'):
Model::factory('tableOne')
->select('*')
->select('tableOne.id', 'id')
->left_outer_join('tableTwo', array('tableOne.tableTwo_id', '=', 'tableTwo.id'))
->find_one($id);
if in table primary key/ field name not id then following id column overrides required
default id (primary_key) to replace with other id name (primary_key)
ORM::configure('id_column_overrides', array(
'user' => 'user_id',
));
$update = ORM::for_table('user')->find_one(1);
$update->name = "dev";
try{
$update->save();
}catch(Exception $e){
echo $e;
}
print_r($update);

Laravel update Issue

Here is my code -
$updatecompany = DB::table('Companies')
->where('ID', (int)$companyid)
->update(array(
'CompanyName' => $companyname,
'CompanyAddress' => $companyaddress,
'CompanyEmail' => $companyemail,
'ContactName' => $contactname,
'CompanyCity' => $companycity,
'CompanyState' => $companystate,
'CompanyZip' => $companyzipcode,
'CompanyPhone' => $companyphone,
));
$updatecompany is always 0. What might be the problem?
One of most possible reasons is that you are updating with the same data in the database.
There needs one out of the box solution, of course if you can do it.
So, no rows are updating, even if the SQL is correct.
Here is my suggestion:
Add a new column updatedOn in DB Table Companies.
The type should be TIMESTAMP and add attribute ON UPDATE CURRENT_TIMESTAMP.
This way you will always get row affected and hence you get return value other than 0.
You don't need to cast $companyId to an integer there. It does not help Laravel's query builder.
Use dd($companyId) and dump the variable before you run the query and find out what it is.

How can I use insertFromSelect(...) without 'on duplicate key update'?

I'm modifying Createat.php, you can see the source here.
Quick overview:
$select->from(array('o' => $this->getTable('sales/order')), $columns)
->join(array('oi' => $selectOrderItem), 'oi.order_id = o.entity_id', array())
->where('o.state NOT IN (?)', array(
Mage_Sales_Model_Order::STATE_PENDING_PAYMENT,
Mage_Sales_Model_Order::STATE_NEW
));
...
$select->where('o.channel_name IS NULL');
$adapter->query($select
->insertFromSelect($this->getMainTable(), array_keys($columns)));
So everything is good at this point. My sales_order_aggregated_created table now contains all "Website" sales totals.
$select->reset('where');
$select->where('o.channel_name = ?', 'Amazon');
$adapter->query($select
->insertFromSelect($this->getMainTable(), array_keys($columns)));
This is where my problem is, when I reset('where') and filter by another channel_name and try to insert new rows into the table the insertFromSelect() function is using "INSERT ... ON DUPLICATE KEY UPDATE" and overwrites the values inside table from the previous query.
Is there a $select->someFunction() that does not update on duplicate or where would I find the definition of insertFromSelect() function? Or, perhaps there is another way of doing this?
Varien_Db_Adapter_Interface has a constant INSERT_ON_DUPLICATE which you can pass as a flag to insertFromSelect() to disable the default ON DUPLICATE KEY UPDATE behavior.
$adapter->query(
$adapter->insertFromSelect(
$select,
$this->getMainTable(),
array_keys($columns),
Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE
)
);

Categories