DB Error when running python manage.py loaddata datadump.json - mysql

I'm migrating my Django database from sqlite to mysql. I've done the following with no problems:
python manage.py dumpdata > datadump.json
Change your settings.py to the mysql database.
But when I issue the following command python manage.py loaddata datadump.json I get this error:
IntegrityError: (1062, "Duplicate entry '13-13' for key 'from_category_id'")
Can someone tell me how to go about fixing this issue so that I can run the command again and hopefully load my data?
Thanks,
J.

Do you have existing data in theDB?,
try with indent --4 to get a version that you can eyeball
Post some sample data
It looks like you got a duplicate key violation or your data you are trying to insert does not match the column type i.e check the constraints applied in your models.py, field types, and the tables created in mysql
The question then will be why does it work in SQLITE and not my sql?
Simple really, SQLITE does not do any type checking, i.e you could easily insert text into an integer field. You will need to clean your data before inserting it into MySQL.
Unlike most SQL databases, SQLite does
not restrict the type of data that may
be inserted into a column based on the
columns declared type. Instead, SQLite
uses dynamic typing.
From http://www.sqlite.org/lang_createtable.html

Related

Migrate existing database fields.E340 error on run

Hey everyone I'm fairly new to Python Anywhere, I'm running a django app on python 3.7, I've manually added the tables and fields I need via SSH in MySQL Workbench and ran [migrate.py inpectdb > /app/models.py] then makemigrations then when I run Migrate I get this:
ERRORS: auth.Group.permissions: (fields.E340) The field's intermediary table 'auth_group_permissions' clashes with the table name of 'app.AuthGroupPermissions'. auth.User.groups: (fields.E340) The field's intermediary table 'auth_user_groups' clashes with the table name of 'app.AuthUserGroups'. auth.User.user_permissions: (fields.E340) The field's intermediary table 'auth_user_user_permissions' clashes with the table name of 'app.AuthUserUserPermissions'.
If I remove the auth tables from the models.py and try to migrate I get:
File "/usr/lib/python3.7/site-packages/MySQLdb/connections.py", line 276, in query _mysql.connection.query(self, query) django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')
From what I've read it is conflicting with the settings.py [INSTALLED_APPS] but I'm not sure where to go from here to get the migration to work properly.
The extra tables that you noticed were created by Django because you have the app that creates those tables enabled in your INSTALLED_APPS. From the table names involved, I'm guessing it's django.contrib.auth that's adding them. There are probably other tables that are being created that way, but they are just not clashing with the tables you've already created.
The second error you're getting is because you have tried to create a key on a column (or columns) that is too big to be a key. That may still be as a result of the auth_ tables clashing. For instance, the Django model may be specifying a key on the id of a table, expecting it to be an integer column, but your database has a large string column for id instead.
I suspect that you may continue to have issues as long as you try to get the Django database and your database to be in the same database. Django does, however, support multiple databases so you could put your legacy database in one database and have your Django database in another. That way, they have no way on stepping on each other.

mysqldump restore issues

i am trying to create a backup of my db.
i have separated the schema and the data in two different sql files from mysql dump.
when i try to restore the schema works fine, but when i execute the data.sql file i get too many errors.
sql error
please check the link for the error
Your problem might be that you are trying to insert data into some columns which reference to another column which is not populated yet thereby the query is failing on integrity checks. I suggest you create one sql dump for both the schema and data and run it once. That may save you this headache.

Deleting a table from MySQL database in Django manually

During my experimentation with my blog app (blogapp) in Django, I created two models (Category and Language), connected them to another model (Post) using following connections:
category = models.ManyToManyField(Category)
language = models.ForeignKey(Language)
Then it gave an error like THIS due to the lack of default value. Tried to roll that back by using an amalgam of THIS and THIS. Then I tried to add a default value using THIS. I've got an error "django.db.utils.OperationalError 1050, Table XXX already exists", then I tried THIS. Tried to revert back migrations by deleting the created migrations from the migrations folder manually. At some point I got django (1054, "Unknown column in 'field list") error.
Finally I decided to revert back to my original starting place. When I connect to my MySQL database using python manage.py dbshell, I realized that my MySQL server still have two tables that should have been deleted, blogapp_category and blogapp_language. Server is working properly but I keep getting "Table XXX already exists" error when I try to add those models.
Dropping tables from MySQL seems to be the only option at the moment.
When I run
mysql> SHOW COLUMNS FROM blogapp_post;
I did not see any reference to language or category, i.e. no columns named language_id or category_id. I have two questions at the moment:
Is it safe to delete tables manually using:
DROP TABLE blogapp_language;
DROP TABLE blogapp_category;
Will there be any negative effects?
Is there a way to freeze database like git so that when I revert to the old database, such tables added to the database by django migrations automatically dropped??
Delete respective entry from table django_migrations.
Delete migration folder from your app.
Delete table created by the app.
Now do makemigrations and migrate.
You can revert back using git but there will be errors and data correction requirements.

liquibase creates duplicate sequence IDs in postgresql database

i am an amateur with postgresql and a newbie with liquibase.
i am using puppet and liquibase to create postgresql database on rhel server.
i drop the database (puppet resource postgresql_database ensure=absent) then run puppet to re-create the database.
i log into psql and run \dt \di \ds. no duplicate tables or indexes but duplicate sequences e.g.
activity_log_activities_id_seq
activity_log_activities_id_seq1
the baseline.xml lists the sequence 1 time e.g.
<createSequence sequenceName="activity_log_activities_id_seq"/>
<createSequence sequenceName="activity_log_activity_products_id_seq"/>
i've google'd liquibase duplicate sequences id1 etc. but no good hits.
please advise.
we didn't figure out what was creating the sequences the 1st time but we did resolve it via removing the creation of the sequences within the baseline.xml included.
now i'm attempting to figure out why when i add includeAll with path="sqls" and run java -jar ../liquibase.jar --changeLogFile=master.xml update
it comes back with "Liquibase Update Successful"
but it's not making a change to the database or the databasechangelog table. i'll do a search and see if it's already been answered elsewhere... thanks!

How can I update mysql database schema in Symfony2

Can anybody help me? How can i update mysql database schema (entity, doctrine yml etc) after inserting new integer field type?
I added manually a new field into mysql, I get semantical error like this :
Site\SiteBundle\Entity\Yorum has no field or association named yayin
using cmd or a console
go to the root of the project
and type
php app/console doctrine:schema:update --force
and remember to clear up your cache
NOTE: You shouldn't do the changes directly into mysql, add the field into the entity and remember to put the right annotations with the field type, length, etc, when you do the schema:update, that would update the sql schema and you'd probably avoid getting this kind of error messages
Check the docs for more info on this subject
Do you have any relations between two tables?
Because the error
Site\SiteBundle\Entity\Yorum has no field or association named yayin
This error is thrown when there is relation between tables