How to migrate existing tables using Django and Python… here is a solution to the problem.
How to migrate existing tables using Django and Python
I need a help. I have an existing mysql table in my localhost database and I need it to be migrated using Django and Python. Here is my code :
Set up .py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangotest',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
I give my table structure below.
People:
id name phone age
Model .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()
I’m actually new to Django and Python, and here I need to know the commands that can migrate existing tables.
Solution
To create a migration, you need to use this command –
python manage.py makemigrations
The above command creates a file in the migrations folder in your application directory, and
Create/update tables using migration files from the database
python manage.py migrate
The above command will create/update a table in your database.
If this is what you want, please let me know!