首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用"active"::boolean更改列"active“类型的boolean

使用"active"::boolean更改列"active“类型的boolean
EN

Stack Overflow用户
提问于 2020-09-24 07:46:04
回答 1查看 62关注 0票数 0

我目前正在阅读Antonio Mele的"Django 3 By Example“。第3章让我完成创建博客应用程序的工作。在本章中,它让我将数据库从MySQL更改为PostgreSQL,然后迁移数据。当我迁移数据时,我得到这个错误:

代码语言:javascript
复制
**django.db.utils.ProgrammingError: cannot cast type timestamp with time zone to boolean
LINE 1: ..." ALTER COLUMN "active" TYPE boolean USING "active"::boolean**

我已经阅读了一些PostgreSQL文档,并重新阅读了我的代码,但是由于这个错误,我无法完成迁移。我已经被困在这个地方很长一段时间了,似乎不能孤立地纠正错误。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

发布于 2020-09-25 07:49:19

我想我可能已经解决了我的问题,“active”是一个BooleanField,而迁移将其转换为DateTimeField。

代码语言:javascript
复制
# 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的输出:

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64037699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档