How to use Django with an existing database in MySQL?

1

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'm creating.

Use the command

python manage.py inspectdb > models.py

To create the models.py file which is cleaned as indicated by the models.py file that was generated.

# Este es un módulo de modelo de Django generado automáticamente.
# Tendrás que hacer lo siguiente manualmente para limpiar esto:
# * Reordenar el orden de los modelos
# * Asegúrese de que cada modelo tenga un campo con primary_key = True
# * Asegúrese de que cada ForeignKey tenga 'on_delete' establecido en el comportamiento deseado.
# * Elimine las líneas 'managed = False' si desea permitir que Django cree, modifique y elimine la tabla
# Siéntase libre de cambiar el nombre de los modelos, pero no cambie el nombre de los valores de db_table o los nombres de campo.

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.

    
asked by jhon1946 09.04.2018 в 19:00
source

1 answer

1

You can integrate your existing data base with the following steps, although you have already done them but this way it has worked for me:

  • Create a Django project by running django-admin.py startproject myapp (where myapp is the name of your application).
  • Edit the configuration file in that project, mysite/settings.py , to tell Django what are the parameters of connection to your database and what is its name. Specifically, it provides the configurations of :
  •   

    DATABASE_NAME, DATABASE_ENGINE, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, and DATABASE_PORT.

  • Create an application within your project by running python mysite/manage.py startapp myapp (where myapp is the name of your application).

  • Execute the python mysite/manage.py inspectdb command. This will examine the tables in the DATABASE_NAME database and print the generated class model for each table. Take a look at the exit to get an idea of what inspectdb can do.

  • Save the output in the file models.py within your application using the standard output redirection of the shell:

    $ python mysite/manage.py inspectdb > mysite/myapp/models.py
    
  • For the error you are giving you, you can use this code to mark the migrations as executed without actually executing them.

      

    (1050, "Table 'XXXXXXX' already exists")

    Use the following command:

    python manage.py migrate --fake <el nombre del proyecto>
    

    or you can use this command:

    python manage.py help migrate
    

    You can read more about this error in this link django doc

        
    answered by 09.04.2018 / 19:20
    source