Integrity error django - mysql

I was using sqlite3 for my database in django project and then I decided to change it to mysql engine. I changed everything in the settings.py correctly, and in order not to lose the db I dumped my sqlite one to text and I changed every " with `, removed Commit and all things related to sqlite3. I executed the dump file on mysql and everything was added perfectly. However, when I try to add a cateogory in a table called categories.
I get this error message
IntegrityError at /admin/red_carpet/category/
(1062, "Duplicate entry '0' for key 1")
I thought the key is duplicate wich what is the error saying. I looked at the "id" column to see it is structure I found that the id column is "NOT NULL". There was no auto increment added to that column so I added it and tried to do category from admin page I got the same error I looked again at the structure and then I added the query that the auto_increment shouldn't start from 0 but should start from last value which was 6. Then I added the category from the django admin again but I still get the same error, looked again at the structure and especially in the indexes part in phpmyadmin I found that line
Keyname Type Cardinality Action Field
PRIMARY PRIMARY 9 id
For me, everything looks good and actually when I add value through django admin I get it added though I get the same error message
IntegrityError at /admin/red_carpet/category/
(1062, "Duplicate entry '0' for key 1")
I really can't understand why is this problem !!!! Anyone have been thorough this before !! ??
PS: I get the same error message when I try to remove something. For example, if I am removing a cateogory I get the exat error message though when I look at the db I found that the category has been removed !!

One way to prevent cross-db issues would be to use fixtures, e.g. dump and load your data in (db-agnostic) json format:
see dump data command and
what is a fixture
from the django documentation.

Related

TypeORM: duplicate entry error after changing length of primary key

I am making a table for an email with Nestjs, MySQL, and Typeorm. I have char type primary key length of 39, and have put a unique string made up of UUID concatenate with some extra sting in it.
Here is the way I make the primary key with Typeorm in Nestjs entity file named 'email'. There is no #PrimaryColumn or #PrimaryGeneratedColumn in it.
#Column({
primary: true,
type: 'char',
length: 39,
})
id: string;
There were some data in the table. After I changed the length from 39 to 45 and run the server, an error happened at TypeOrmModule. I didn't try to save anything. It's strange that I have seen similar errors during inserting data thru googling.
QueryFailedError: Duplicate entry '' for key 'email.PRIMARY'
Even I set back the length to 39, still the error happens. The only way that I've found to run the server is by dropping the table and make a new one.
I have 2 questions about this error.
Is there a solution to change the length of the primary key, keep the data in the table, and run the server?
How '' - an empty string from the error sentence - affects the uniqueness of the primary key?
My guess is that you have synchronize: true in your Typeorm config object.
And based on Typeorm documents
Indicates if database schema should be auto created on every application launch. Be careful with this option and don't use this in production - otherwise you can lose production data.
So what actually happens is that when you change the length of a column and launch your application typeorm tries to sync this change and update your schema. And for the reasons mentioned here, Typeorm does that by dropping the column entirely and creating a new one, where it encounters a duplicate error.
For your first question, you can use migrations to have more control over how the schema is updated. Though I should mention that in migration typeorm behave similarly and drops the column for the change, but at least you can edit the migration file yourself to ensure it does what you want it to do. Or you can edit the schema of database directly. Whatever you choose to do, synchronize probably is never a good idea, except for the very early stages of development.
As your second question, since your primary column is a char with no default value, the new column values will be filled with '' noramally, as default of char for fields that are not nullable; but obviously it encounters a duplicate error on a primary column where uniqueness is a most too; hence the error Duplicate entry '' for key 'email.PRIMARY'

Show human error if inserting duplicate data in access database

I've prohibited duplicate records in access database so no one will insert same data again and again. it works fine, but the error message is not helpful to the users, i want to show that error in a more human way possible.
Microsoft JET Database Engine (0x80004005)
The changes you requested to the table were not successful because
they would create duplicate values in the index, primary key, or
relationship. Change the data in the field or fields that contain
duplicate data, remove the index, or redefine the index to permit
duplicate entries and try again.
Instead of this lengthy message i just want to show "Sorry - Record already exist."
Is this possible?
Two ways to do this:
Catch the error. I think classic ASP requires on error resume next and then you need to check the err message. You might even have to match the string since I think the error code will be the same for different errors.
Do a select from the database before the insert to check if the value already exists
Either way you can then display your own message. Might be even more user-friendly to customize the message further ("Sorry - species 'Gruffalo' already exists").

Magento re-index, cannot create table

I'm trying to re-index the category flat data, but I am always met with the same error:
There was a problem with reindexing process. Error Message: SQLSTATE[HY000]: General error: 1005 Can't create table 'xxx.catalog_category_flat_store_6' (errno: 121)
The table doesn't exist, there is a 1 and a 7. Not sure if that makes a difference?
After running the query manually through phpMyAdmin, I am met with the MySQL error 121. I've checked around and this would suggest the names of the foreign keys trying to be created already exist. I've listed all foreign keys in the DB right now, and they don't exist at all.
I've also tried running SHOW ENGINE INNODB STATUS on the DB for more information, but we don't have the rights to view that apparently.
After getting the priv's updated so we could run SHOW INNODB STATUS, we discovered that we already had an existing index that was attempting to be duplicated with this new table. This stemmed from us backing up an older version of the table that was trying to be created. Deleting that copy of the table enabled Magento to re-index properly and solved our problem.
Try logging the sql commands and debug what its trying to do by executing them manually. On the index process, normally there is a command that clears a table, and another to recreate it.
Edit /magentoRoot/lib/Varien/Db/Adapter/Pdo/Mysql.php and change $_debug to true and note the $_debugFile location (should be var/debug/pdo_mysql.log)
Its best to edit the file in vi, have a browser open to reindex JUST the category data, save the file in vi :w! and then run the indexer then change the debug back to false.
Then go read the log. It may help.

magento can't reindex product attributes

When I run Reindex from Index Management it gives error "Cannot initialize the indexer process."
So based on following article here
http://jasonehmke.com/ecommerce/cannot-initialize-the-indexer-process-in-magento/
I ran
php -f indexer.php reindexall
Which gave following output:
Product Prices index was rebuilt successfully
Catalog URL Rewrites index was rebuilt successfully
Product Flat Data index was rebuilt successfully
Category Flat Data index was rebuilt successfully
Category Products index was rebuilt successfully
Catalog Search Index index was rebuilt successfully
Stock Status index was rebuilt successfully
Tag Aggregation Data index was rebuilt successfully
Product Attributes index process unknown error:
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '65-542-1-65' for key 'PRIMARY'' in /home/bizzosph/public_html/lib/Zend/Db/Statement/Pdo.php:228
Next exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '65-542-1-65' for key 'PRIMARY'' in /home/bizzosph/public_html/lib/Zend/Db/Statement/Pdo.php:234
Any idea how do I fix this? It does not tell me what table has duplicate entry.
Thanks
I'm sure you've moved on since posting this question, but since it has ~2500 views (and having this answer here would have helped me), I'll add the resolution I found. This does not require truncating any database tables or even any direct SQL queries:
Last night, I was having the same problem. My error was the same as yours (except for the numbers which refer to the product and attribute causing the problem). If you go and re-save that product in the Manage Products section of the admin (no need to change it, just open it to edit and click save), it will solve the problem - but you very likely have more products that will bring additional similar errors.
In my case, it wasn't just a problem with 1 product there were several hundred products causing errors (each one saved brought up a new one). And it wasn't all with the same attribute (I'd updated multiple attributes across a database of over 4,000 products in my last import).
Since everything seemed to actually be in the database correctly (since re-saving seemed to be fixing it and giving the proper data to the final saved product), I had an idea.
Why not export all 4k products, and then re-import the same file without modification and see if that clears it.
It worked!
tl;dr: If you're having this problem with multiple products needing to be re-saved, export your whole inventory (or the relevant section if you can segment in a useful way), and re-import the same file with no modifications.
I'm using Magento v 1.9.0.1
Try first to clean Magento cache. If no success, run official DB repair tool
The solution is to run the following MySQL queries:
TRUNCATE TABLE `catalog_product_flat_1`;
TRUNCATE TABLE `catalog_product_flat_2`;
TRUNCATE TABLE `catalog_product_flat_3`;

Duplicate entry '1' for key 'PRIMARY'

I just did a sql dump using a python script and compressed it. When I tried to restore the same in my local machine I am getting an error "Duplicate entry '1' for key 'PRIMARY'". But I have created a separate test database, which is empty.
Whats the problem and what would be the solution?
I suppose your dump contains database name inside (something like USE your_database), so when you try to restore it you get that error.
Another condition could be a wrong dump: did you check your dump does not contain really duplicate keys?
And more: do your dump have DROP TABLE ... IF EXISTS.. ?