django项目的模型迁移出错

在生成迁移文件时特别正常,但是迁移到数据库的时候也没有报错,只是没有生成数据表而已,实际问题出现在你的模型语句相关有问题,代码如下:

class Article(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='id')
    title = models.CharField(max_length=500,verbose_name='文章标题')
    username = models.CharField(max_length=100, blank=True, null=True)
    text = models.TextField()
    date = models.DateTimeField(blank=True, null=True)
    tag1 = models.CharField(max_length=255, blank=True, null=True,verbose_name='引言')
    type_num = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'article'
        verbose_name = '文章内容'
        verbose_name_plural = verbose_name
        ordering = ['title']

 managed = False 是MySQL不支持的(我猜测)

处理方案:

执行makemigrations后生成迁移文件0001_initial.py,打开将 managed = False注释,如下:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.21 on 2021-07-27 19:04
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Article',
            fields=[
                ('id', models.AutoField(primary_key=True, serialize=False, verbose_name='id')),
                ('title', models.CharField(max_length=500, verbose_name='文章标题')),
                ('username', models.CharField(blank=True, max_length=100, null=True)),
                ('text', models.TextField()),
                ('date', models.DateTimeField(blank=True, null=True)),
                ('tag1', models.CharField(blank=True, max_length=255, null=True, verbose_name='引言')),
                ('type_num', models.CharField(blank=True, max_length=255, null=True)),
            ],
            options={
                'verbose_name': '文章内容',
                'verbose_name_plural': '文章内容',
                'db_table': 'article',
                'ordering': ['title'],
                # 'managed': False,
            },
        ),
]

注释之后,执行migrate,完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值