Steps to change table and column names in your Django model at the same time

Steps to change table and column names in your Django model at the same time

Change before

class AAA(models.Model):
    name = models.CharField()
    
    class Meta:
        db_table = 'aaa'

class BBB(models.Model):
    name = models.CharField()
    aaa = models.ForeignKey(AAA)
    

After change

class CCC(models.Model):
    name = models.CharField()

    class Meta:
        db_table = 'ccc'

class FooTwo(models.Model):
    name = models.CharField()
    ccc = models.ForeignKey(CCC)

Correction procedure

(1). Models.py modified

class CCC(models.Model): <-Fixed AAA to CCC
    name = models.CharField()

    class Meta:
        db_table = 'ccc' <-Modify aaa to ccc

class FooTwo(models.Model):
    name = models.CharField()
    aaa = models.ForeignKey(CCC) <- ForeignKey(AAA)Foreign Key(CCC)Fixed to

(2). Modify the migration file below after schemam migration

    def forwards(self, orm):
        db.rename_table('aaa', 'ccc')

    def backwards(self, orm):
        pass

(3). migrate --Check if the table name has changed AAA-> CCC

(4). Modify models.py

class CCC(models.Model):
    name = models.CharField()

    class Meta:
        db_table = 'ccc'

class FooTwo(models.Model):
    name = models.CharField()
    ccc = models.ForeignKey(CCC) <-Modify aaa to ccc

(5). Modify the migration file below after schemam migration

    def forwards(self, orm):
        db.rename_column('ccc', 'aaa', 'ccc')

    def backwards(self, orm):
        pass

(6). migrate --Check if the column name has changed aaa-> ccc

Recommended Posts

Steps to change table and column names in your Django model at the same time
Browse .loc and .iloc at the same time in pandas DataFrame
Loop variables at the same time in the template
I want to make a music player and file music at the same time
Make sure to align the pre-processing at the time of forecast model creation and forecast
Visualize data and understand correlation at the same time
I tried to illustrate the time and time in C language
[Django] Field names, user registration, and login methods that can be used in the User model
Display the time in Django according to the user's region (UTC)
The story of returning to the front line for the first time in 5 years and refactoring Python Django
How to return the data contained in django model in json format and map it on leaflet
Determine the date and time format in Python and convert to Unixtime
How to display bytes in the same way in Java and Python
How to read standard input or variable files at the same time like paste command in Python
How to get the date and time difference in seconds with python
Added a function to register desired shifts in the Django shift table
I implemented the VGG16 model in Keras and tried to identify CIFAR10
Plot multiple maps and data at the same time with Python's matplotlib
[Python] How to open two or more files at the same time
9 Steps to Become a Machine Learning Expert in the Shortest Time [Completely Free]
How to count the number of elements in Django and output to a template
Implement the ability to reserve what happens regularly in your Django Todo list
Instantly convert Model to Dictionary with Django and initialize Form at explosive speed
I'm addicted to the difference in how Flask and Django receive JSON data
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
Turn multiple lists with a for statement at the same time in Python
12. Save the first column in col1.txt and the second column in col2.txt
Specify the view URL in your Django template
To represent date, time, time, and seconds in Python
Cisco IOS-XE captures changes in the routing table and posts them to external services
[Note] How to write QR code and description in the same image with python
I just wanted to extract the data of the desired date and time with Django
Tutorial to infer the model learned in Tensorflow with C ++/OpenVINO at high speed
Memorandum (in openpyxl ① copy and paste from another book ② refer to the comparison table)
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row