我目前正在阅读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 USING "active"::boolean**我已经阅读了一些PostgreSQL文档,并重新阅读了我的代码,但是由于这个错误,我无法完成迁移。我已经被困在这个地方很长一段时间了,似乎不能孤立地纠正错误。任何帮助都是非常感谢的。
发布于 2020-09-25 07:49:19
我想我可能已经解决了我的问题,“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;https://stackoverflow.com/questions/64037699
复制相似问题