3

I need one help. I have one existing mysql table in my localhost database and I need it to migrate using Django and Python. Here is my code:

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangotest',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

I am giving my table structure below.

Person:

id    name  phone  age

models.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Person(models.Model):
    name = models.CharField(max_length=200)
    phone = models.CharField(max_length=15)
    age = models.IntegerField()

Actually I am new to Django and Python and here I need to know command which can migrate the existing table.

0

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

6 Comments

@satya , is this what you are looking for.
Actually I have one existing table in my DB. I need to migrate that.
See my post .I want to say I have created one table in localhost/phpmyadmin and I need to use that in my app.Actually I am new to Django and Python and I am making one crud application.
Actually I run python manage.py sql crud and it gave me Unknown command: 'sql' Type 'manage.py help' for usage..
if you have created a table already in you db (e.g mysql) , then you don't need to run steps I mentioned above unless you need to update some columns etc , and also you have added table schema in models.py , I think it would be sufficient , you just start using it without any migrations .
|
1

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).

Comments

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'

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.