[Django]-How to migrate existing table using Django and Python

2👍

to create migrations you need to use this command –

   python manage.py makemigrations

the above command will create a file inside the migrations folder in your app directory and
to create/update table using the migration file in the database

  python manage.py migrate

The above command will create/update a table in your DB.

Django Migration Docmentation

Let me know, if this is what you want!

1👍

In reference to akhilsp, I did not have to worry about table names using the _ format. Using the inspectdb command returned a Meta data for whatever the current table name is that I used in my model.

class Meta:
    managed = False
    db_table = 'malware'

0👍

You can use inspectdb command from the shell.

python manage.py inspectdb

This will print models corresponding to the current database structure. You copy the required model, make changes like adding validations, and then add other models.

python manage.py makemigrations will create migrations, and

python manage.py migrate will apply those migrations.

N.B: Your table name should be in the format “appname_modelname” where appname is name for django app name (not project name).

0👍

Add --fake option to migrate command:

–fake

Tells Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your
database schema
.

This is intended for advanced users to manipulate the current
migration state directly if they’re manually applying changes; be
warned that using –fake runs the risk of putting the migration state
table into a state where manual recovery will be needed to make
migrations run correctly.

If you just start django project and didn’t have initial migration: comment your Person model, make initial migration, apply it, uncomment Person, make migration for Person and at last migrate Person with --fake option.

👤ndpu

Leave a comment