简体   繁体   English

添加新模型后。 如何在不丢失数据的情况下进行 Django 表迁移

[英]After Add New model.How to Django Table Migrate without losing data

class UserSubStatus(models.Model):
    msisdn = models.CharField(max_length=200)
    category = models.CharField(max_length=200, null=True, blank=True)
    validity = models.IntegerField(default=1,null=True,blank=True)
    sub_status = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-created_at']
        db_table = "user_sub_status"

I add this table on my model.py file.我将此表添加到我的 model.py 文件中。 How add this "user_sub_status" table on my database without losing any data.如何在我的数据库中添加这个“user_sub_status”表而不丢失任何数据。

run makemigrations and migrate as you did before.像以前一样运行 makemigrations 和 migrate 。 If you do not erase previous migrations files then by running makemigrations new migrations files will add to migrations folder and by migrate new tables or columns for previous tables will add to your database and there will be no problem with your stored data如果您不删除以前的迁移文件,那么通过运行 makemigrations 新的迁移文件将添加到迁移文件夹中,并且通过迁移新表或以前表的列将添加到您的数据库中,并且您存储的数据不会有问题

First, you have to run makemigrations command which is responsible for creating new migrations based on the changes you have made to your models.首先,您必须运行 makemigrations 命令,该命令负责根据您对模型所做的更改创建新的迁移。

python manage.py makemigrations

Then you have to run a migration command which is responsible for applying and unapplying migrations that are created by the above command.然后,您必须运行一个迁移命令,该命令负责应用和取消应用由上述命令创建的迁移。

python manage.py migrate

With the help of above both command, your table "user_sub_status" will be added in your database without losing any data from your database.在上述两个命令的帮助下,您的表“user_sub_status”将添加到您的数据库中,而不会丢失数据库中的任何数据。

Hello Dear你好呀

First run the following command:首先运行以下命令:

python manage.py makemigrations your_app_name

than run this command:比运行这个命令:

python manage.py migrate your_app_name

NB: I hope, "user_sub_status" will be added to your database without losing data.注意:我希望“user_sub_status”将被添加到您的数据库中而不会丢失数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 迁移将 Heroku Django 应用程序的新模型表添加到远程 Heroku Postgres? - How to add a Heroku Django app's new model table to the remote Heroku Postgres with Python migrate? 如何在Django admin中添加表而无需迁移命令 - How to add table in Django admin without migrate command 将Django数据移动/迁移到新表中 - Move /migrate django data into new table 如何将 Django model 迁移到带有表 inheritance 的模型中 - How to migrate a Django model into models with table inheritance Django 1.5:如何使用South向现有表中添加新模型字段而不必删除表? - Django 1.5: How do I add a new model field to an existing table using South without having to drop a table? 如何在不丢失数据的情况下更改 model class 名称? - How to change model class name without losing data? 如何在 django 中从 postgresql 删除表后使用迁移创建表? - How to create table with migrate after drop table from postgresql in django? 如何在不换行的情况下向sqlite 3中的表添加数据 - how to add data to a table in sqlite 3 without going to a new line 如何在不重新运行程序的情况下在 tkinter 表上显示新添加的数据? - How to display new add data on tkinter table without rerun the program? 如何将datetimefield添加到Django并迁移 - How to add datetimefield to Django and migrate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM