我运行了makemigrations和migrate,但我的列没有反映在PostgreSQL数据库中

bfrts1fy  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(151)

我是Django的新手,我的版本是4.1。我在pgAdmin4中更改id列的字段类型时遇到了问题。但是我通过在midel.uuidfield中使用id进行迁移来覆盖ID,如下所示:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
import uuid

class Profile(AbstractBaseUser):
    # id = None 
    id = models.UUIDField(unique=True, editable=False, primary_key=True,      verbose_name='ID')
    password = None

当我运行makegrations并在迁移之前将id从None更改为models.uuidfield时,这终于起作用了。现在,该列和添加的其他列都没有反映在数据库中,即PgAdmin4中。
我尝试通过添加一个新列created_time来进行故障排除,如下所示:

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
import uuid

class Profile(AbstractBaseUser):
    # id = None 
    id = models.UUIDField(unique=True, editable=False, primary_key=True, verbose_name='ID')
    password = None
    created_time = models.DateTimeField(auto_now_add=True)

现在,在运行makemigrations和migrate之后,添加到数据库中的唯一记录是created_time。所以我检查了migrations文件夹,在我的0001_initial.py file中,它看起来像这样:

# Generated by Django 4.1.7 on 2023-04-04 07:11

from django.db import migrations, models

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Profile',
            fields=[
                ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
                ('id', models.UUIDField(editable=False, primary_key=True, serialize=False, unique=True, verbose_name='ID')),
                ('firstname', models.CharField(blank=True, max_length=200, null=True, verbose_name='the first name of a user')),
                ('lastname', models.CharField(blank=True, max_length=200, null=True, verbose_name='the last name of a user')),
                ('display_pic', models.ImageField(blank=True, default='images/avatar.jpg', null=True, upload_to='photo/%Y/%m/%d', verbose_name='profile picture')),
                ('username', models.CharField(blank=True, max_length=200, null=True, unique=True, verbose_name='username')),
                ('email', models.CharField(blank=True, max_length=250, null=True, unique=True, verbose_name='email')),
                ('bio', models.TextField(blank=True, null=True, verbose_name='bio of user')),
                ('is_email_verified', models.BooleanField(default=False, help_text="this designates if the user' email is verified")),
                ('is_active', models.BooleanField(default=True, help_text='this designates if the user is active')),
                ('is_staff', models.BooleanField(default=False, help_text='this designates if the user is staff or not')),
            ],
            options={
                'unique_together': {('id', 'email', 'username')},
            },
        ),
    ]

但是在我添加了created_time字段之后,新的迁移文件是这样的:

# Generated by Django 4.1.7 on 2023-04-04 07:19

from django.db import migrations, models
import django.utils.timezone

class Migration(migrations.Migration):

    dependencies = [
        ('subscribers', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='profile',
            name='created_time',
            field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
            preserve_default=False,
        ),
    ]

很明显,最初的迁移没有应用任何更改。
有人能帮我解决吗?

xuo3flqw

xuo3flqw1#

我有一个更新。所以我发现唯一有效的解决方案是删除整个数据库或创建一个新的数据库并将其连接到Django settings.py文件中。然后我对id和其他字段进行了所有修改。我运行makemigrationsmigrate,现在表正在反射。
我希望这对其他人有帮助

相关问题