使用“活动”更改列“活动”类型布尔值:

2024-05-23 17:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在通过Antonio Mele的“Django 3 By Example”进行工作。第三章介绍了如何创建博客应用程序。在本章中,我将数据库从MySQL更改为PostgreSQL,然后迁移数据。迁移数据时,会出现以下错误:

**django.db.utils.ProgrammingError: cannot cast type timestamp with time zone to boolean
LINE 1: ..." ALTER COLUMN "active" TYPE boolean USING "active"::boolean**

我已经阅读了一些PostgreSQL文档并返回了我的代码,但是由于这个错误,我无法完成迁移。我已经在这个地方呆了很长一段时间,似乎无法找出错误的纠正方法。感谢您的帮助


Tags: 数据django数据库应用程序dbbyexamplepostgresql
1条回答
网友
1楼 · 发布于 2024-05-23 17:26:52

我相信我可能已经解决了我的问题,“active”是一个布尔字段,迁移将其转换为DateTimeField

# Generated by Django 3.1 on 2020-08-18 23:58

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

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

    operations = [
        migrations.CreateModel(
            name='Comment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=80)),
                ('email', models.EmailField(max_length=254)),
                ('body', models.TextField()),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                **('active', models.DateTimeField(default=True))**,
                ('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='blog.post')),
            ],
            options={
                'ordering': ('created',),
            },
        ),
    ]

以下是sqlmigrate输出:

BEGIN;
 
  Create model Comment
 
CREATE TABLE "blog_comment" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(80) NOT NULL, "email" varchar(254) NOT NULL, "body" text NOT NULL, "created" timestamp with time zone NOT NULL, "updated" timestamp with time zone NOT NULL, "active" timestamp with time zone NOT NULL, "post_id" integer NOT NULL);
ALTER TABLE "blog_comment" ADD CONSTRAINT "blog_comment_post_id_580e96ef_fk_blog_post_id" FOREIGN KEY ("post_id") REFERENCES "blog_post" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "blog_comment_post_id_580e96ef" ON "blog_comment" ("post_id");
COMMIT;

相关问题 更多 >