[英]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”将添加到您的数据库中,而不会丢失数据库中的任何数据。
First run the following command:首先运行以下命令:
python manage.py makemigrations your_app_name
than run this command:比运行这个命令:
python manage.py migrate your_app_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.