【问题标题】:ALTER COLUMN "active" TYPE boolean USING "active"::booleanALTER COLUMN "active" TYPE boolean USING "active"::boolean
【发布时间】:2021-01-10 05:31:13
【问题描述】:

我目前正在研究 Antonio Mele 的“Django 3 By Example”。第 3 章让我创建了一个博客应用程序。在本章中,我将数据库从 MySQL 更改为 PostgreSQL,然后迁移数据。迁移数据时出现此错误:

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

我浏览了一些 PostgreSQL 文档并返回了我的代码,但由于此错误,我无法完成迁移。我已经被困在同一个地方很长一段时间了,似乎无法隔离对错误的更正。任何帮助表示赞赏。

【问题讨论】:

  • 能否请您包含您的迁移文件以及python manage.py sqlmigrate [your_migration_here] 的输出?

标签: python django postgresql database-migration


【解决方案1】:

我相信我可能已经发现了我的问题,'active' 是一个 BooleanField,迁移将其转换为 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;

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2019-08-06
    • 2013-09-18
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多