Duplicate keys when adding products in Vuetify Data Table - vue.js

I am currently using a Vuetify Data Table where you can add products to. In the end I want to do a calculation based on the price of all those products. The products all have an ID, but when I add those products to the Data Table it gives me the following error:
Duplicate keys detected: '12'. This may cause an update error.
I understand this happens because I use the ID of the product. But how can I prevent that it is giving me this error? Should I pass a completely different ID instead of the one of the product itself? I might need the product ID later. I would like to know what would be the best solution. Thanks in advance.

I suggest you to add a column with an unique id. If you have an array of objects:
array.forEach((item, i) => {
item.subId = i + 1;
})
Then in your Data Table set the item-key to subId.

Related

Product List Position in Google Analytics Event Table

I want to analyze the Product List Position in Google Analytics Event Table. However, during the installation, our Technical Team made a mistake in product list position. Some items' position starts from 0. How to fix this problem with sql? I want to fix only the starting 0 position's list.
Best.
Not sure if I fully understand your issue, but you might insert data to the table and then remove specified rows with 0.
For example:
insert into 'project-name.dataset.table' (select col1, (items.coupom, items.affiliation, items.refund) from 'project-name.dataset.table' where items.refund is null );
It will looks like below:
And then delete unneeded rows
delete from 'project-name.dataset.table' where items.refund is null;
Result will be like below:
Please note that this may process a lot of data and you will have to insert the missing data in the last field that was affected by the error.

Django: How do I update only ONE record in my database?

I'm having a lot of trouble trying to update a single (record) object in my database.
context['eval_list'] = Evaluering.objects.update(eval_relationer_id=self.kwargs.get('pk'))
I use objects.update, but it updates ALL my objects fk. How do I achieve only updating one object? I have also tried this:
context['eval_list'] = Evaluering.objects.update_or_create(eval_relationer_id=self.kwargs.get('pk'))
But this creates a new object and does not update the record that I want to update. I know why it creates a new objects, and it is because the FK I'm trying to update is null. Surely, there must be a way to only update and not create a single record? What am I missing here?
I tried adding a filter, but it feels redundant? I tried this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))
I did consider trying to create an ID of the FK instantly and not later on, but I couldn't really get that to work, but if I created an ID then the update_or_create would work because an ID would exist already, BUT I cannot believe that I can't update a single object without create?
If creating the ID earlier on is the only work around, I will have to figure out how.
MyModel.objects.filter(pk=some_value).update(field1='some value')
The filter gets your object (returns the Queryset with only that object), then the update changes some other field that is not the PK to whatever you want.
In your case probably something like this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(some_attribute='some value')
After help from #Hanny I've figured out was going wrong.
I was trying to filter by the eval_relationer_id, when I should have been filtering by the evaluation pk and getting that specific PK. Otherwise I would be updating ALL the values which is not what I wanted.
So by filtering by pk:
filter(pk=self.kwargs.get('pk'))
And updating by the attribute / fk that I want to update
update(eval_relationer_id=self.kwargs.get('pk'))
This is the end-result:
context['eval_list'] = Evaluering.objects.filter(pk=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))

Magento update customer group prices for products

I'm trying to update the customer group prices in Magento using Mage (as I cannot find a way to do so in the SOAP V2 API). I've found a StackOverflow example, however it doesn't work for me. The code I'm using is as follows:
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$product = Mage::getModel('catalog/product')->load($productID);
$groupPricingData = array(array('price_id'=>1,'website_id'=>0,
'cust_group'=>3, 'price'=>666));
$product->setData('group_price',$groupPricingData);
$product->save();
echo "true";
?>
I get the following error if I use a product that has any customer prices already set. If I try a product with no existing prices, it doesnt error, but the customer group price is not created.
SCREAM: Error suppression ignored for
( ! ) Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with
message 'SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry '24-0-3-0' for key 'CC12C83765B562314470A24F2BDD0F36'' in
C:\wamp\www\magento\app\code\core\Mage\Core\Model\Config.php on line 1348
( ! ) Mage_Eav_Model_Entity_Attribute_Exception: SQLSTATE[23000]:
Integrity constraint violation: 1062 Duplicate entry '24-0-3-0' for
key 'CC12C83765B562314470A24F2BDD0F36' in
C:\wamp\www\magento\app\code\core\Mage\Core\Model\Config.php on line 1348
I have been scouring the web for an answer to how to properly "update" group price on Magento Product records and have been unsuccessful. Pouring over Magento's source code hasn't gotten me anywhere either. I have however, discovered two solutions on how to clear out the existing group price, so that you do not receive this error.
1 - Direct SQL query to remove the existing group price data
$coreResource = Mage::getSingleton('core/resource');
$conn = $coreResource->getConnection('core_write');
$table_name = $coreResource->getTableName('catalog_product_entity_group_price');
$conn->delete($table_name, array('entity_id = '.$product->getId()));
2- Set the group price for the product to be empty, and save the product.
$product->setData('group_price', array())->save();
Performing either of these (no need to do both) PRIOR to setting your group price data and saving the product, will avoid the Integrity constraint violation, since they effectively remove the existing group price for the product. Approach 1 feels sketchy because we usually do not make direct sql queries and instead rely on model classes to have methods already built in place for us to encapsulate this logic, but in my test environment (Magento 1.9) it works. Approach 2 may seem safer, however it is more resource intensive since it is saving the entire product record, when all we really need to do is update/delete the existing group price. Plus it requires a redundant save since we're "saving to remove" the existing group price.
I recommend re-indexing Product Prices afterwards, as group price data is also stored in 'catalog_product_index_group_price'.
Try to use this extension for magmi group prices:
https://github.com/tim-bezhashvyly/magmi-grouped-price-plugin

Database Design: Line Items & Additional Items

I am looking for a solution or to be told it simply is not possible/good practice.
I currently have a database whereby I can create new orders and select from a lookup table of products that I offer. This works great for the most part but i would also like to be able to add random miscellaneous items to the order. For instance one invoice may read "End of Tenancy Clean" and the listed product but then have also an entry for "2x Lightbulb" or something to that effect.
I have tried creating another lookup table for these items but the problem is i don't want to have to pre-define every conceivable item before I can make orders. I would much prefer to be able to simply type in the Item and price when it is needed.
Is there any database design or workaround that can achieve this? Any help is greatly appreciated. FYI I am using Lightswitch 2012 if that helps.
One option I've seen in the past is a record in your normal items table labeled something like "Additional Service", and the application code will recognize this item and also require you to enter or edit a description to print with the invoice.
In the ERP system which we have at work, there is a flag in the parts table which allows one to change the description of the part in orders; in other words, one lists the part number in the order and then changes the description. This one off description is stored in a special table (called NONSTANDARD) which basically has two fields - an id field and the description. There is a field in the 'orderlines' table which stores the id of the record in the special table. Normally the value of this field will be 0, which means that the normal description of the part be displayed, but if it's greater than 0, then the description is taken from the appropriate row in the nonstandard table.
You mean something like this?
(only key attributes included, for brevity)

Duplicate a record and its references in web2py

In my web2py application I have a requirement to duplicate a record and all its references.
For example
one user has a product (sponserid is the user). and this product has so many features stored in other tables (reference to product id).
And my requirement is if an another user is copying this product, the a new record will generate in the product table with new productid and new sponserid. And all the reference table records will also duplicate with the new product id. Effectively a duplicate entry is creating in all the tables only change is product id and sponserid.
The product table fields will change. So I have to write a dynamic query.
If I can write a code like below
product = db(db.tbl_product.id==productid).select(db.tbl_product.ALL).first()
newproduct = db.tbl_product.insert(sponserid=newsponserid)
for field,value in product.iteritems():
if field!='sponserid':
db(db.tbl_product.id==newproduct).update(field=value)
But I cannot refer a field name like this in the update function.
Also I would like to know if there is any other better logic to achieve this requirement.
I would greatly appreciate any suggestions.
For the specific problem of using the .update() method when the field name is stored in a variable, you can do:
db(db.tbl_product.id==newproduct).update(**{field: value})
But an easier approach altogether would be something like this:
product = db(db.tbl_product.id==productid).select(db.tbl_product.ALL).first()
product.update(sponserid=newsponserid)
db.tbl_product.insert(**db.tbl_product._filter_fields(product))
The .update() method applied to the Row object updates only the Row object, not the original record in the db. The ._filter_fields() method of the table takes a record (Row, Storage, or plain dict) and returns a dict including only the fields that belong to the table (it also filters out the id field, which the db will auto-generate).