Django Migrate command throwing ORA-00955 error - python

I have an oracle 12c existing database.
I used inspectdb to create the django models. Most of the tables in the db dont have a primary key and I assume that if pk is not set explicitly, then Django would apply the id column as the primary key to the tables.
Also, I have set managed=True on all my models so, Django should ideally be able to create the id column for its use.
When I run makemigrations, I dont get any error. But when I run migrate after that I get the following error:-
return self.cursor.execute(query, self._param_generator(params))
django.db.utils.DatabaseError: ORA-00955:
name is already used by an existing object
Any clue why I am getting this error?

Update:-
So, I found out that the issue was with the initial migration itself. Since, the tables were already existing in my case, it might have taken it as duplicate object names when trying to migrate the first time. I had to fake the initial migration so that it only checks if the tables exist for the models and if it does then migrate. Below is the command for the same. It worked perfectly and my initial migration was successful.
python manage.py makemigrations
python manage.py migrate --fake-initial

Related

Django - delete M2M field but keep join table

is it possible to remove an M2M field from a model and keep the joining table?
context:
I am trying to add through model to existing M2M field like described in this post
But doing it simply like this will result in a production app crash when accessing the old table during deployment - short window between migration and code update, when old code will try to access a new database for a few moments - without the old table in it.
You can use --fake flag when running manage.py migrate. That will make a migration file that says the model field has been removed and mark it as applied in the database migration table, but not actually execute the SQL to remove the corresponding tables etc. Read more here

Data migrations for OneToOneField in django with data in tables already

I have a case model that I added a OntToOneField to
Example:
zoho_api = models.OneToOneField(
ZohoAPIState,
default=create_new_zoho_api_state,
related_name='referral_profile',
on_delete=models.CASCADE
)
During development there was no data in the tables and the migration worked fine. The migration is adding the zoho_api key to the database using the following commands.
python manage.py makemigrations
python manage.py migrate
But trying to migrate with data in the tables gives me the following errors:
Errors
Says DETAIL: Key(zoho_api_id)=(some guid) is duplicated.
How do I get around this issue?

"Relation ... does not exist" after deleting few tables

I have a postgres database, which naming midgard_dev. It has a many tables.
After changing in models at product app I got error 'Column product_... does not exist'. I removed few tables in database, which related with product, and got error "Relation products_product does not exist. LINE 1: SELECT COUNT() AS "__count" FROM "products_product".
This error I get, when open page with product (from admin-panel or user-page)
I watched migrations (with ./manage.py showmigrations) and saw, that all migrations successful.
makemigrations app and migrate --fake don't help respectively. Delete the entire database don't want.
The tables that remained associated with the product
Thnx for help.
I had same error, I solved it by typing python manage.py makemigrations model_name then python manage.py migrate model_name to command promt/power shell. Reason could be that typing same commands without model_name do not create tables and/or relations for specific model(not professional opinion).model_name is name of your model.

How to use Django with an existing database in MySQL?

I have an application in Django 2.0 and as a database engine I use MySQL. I have a problem because the database was previously created and already has records, my idea is to use this same database for the application I am creating.
Use the command
python manage.py inspectdb > models.py
To create the models.py file which will be cleaned as indicated by the models.py file that was generated.
#This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
# * Rearrange models' order
# * Make sure each model has one field with primary_key=True
# * Make sure each ForeignKey has `on_delete` set to the desired behavior.
# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
After this I proceed to execute:
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
But it generates the following error:
(1050, "Table 'XXXXXXX' already exists")
Obviously it tells me that the table already exists, but how do I not generate this error and continue administering the tables from Django.
You need to run --fake-initial or --fake. See more at Django migrations. Be careful because running inspectdb doesn't solve all your problems. You need to fix the things inside models.py manually and migrate again.
One of the things (and the main reason I do not use Django) is it likes to take control of everything. The fact that it controls the database means that if you don't start strictly in Django, you are doing it wrong.
However there is a work around:
https://docs.djangoproject.com/en/2.0/howto/legacy-databases/

How to change a sqlite table column value type from Django model?

I created a table before I code the Django app and now I merged both the app and the table with following command python manage.py inspectdb > models.py. However after some while I really need to change the value type of one of the column. Is it enough to chage it through the model file or do I need some additional steps?
If you change a field in a Django model, Django itself doesn't know how to update your database accordingly (syncdb only add tables from new models).
You have two options:
manually create your database tables;
use a migration tool like South that detects and generates migration files from changes made to your models;
I recommend the second option as it's programmatic, more error-proof and makes your life easier when you need to go back and forth between database schemas.
There is an easy way to do this. (in Django 2)
After making the necessary changes to the model.py file of your app, run command:
python manage.py makemigrations - This will generate a new file in migration folder of your app.
python manage.py migrate - This will apply those edits on actual databse.
To check if the changes have been applied, run command : .schema <tablename> in your terminal, after entering the sqlite command-line program.

Categories

Resources