简体   繁体   English

django.db.utils.ProgrammingError:运算符不存在:字符更改日期

[英]django.db.utils.ProgrammingError: operator does not exist: character varying date

I am querying from a PostGre db in my Django project. 我正在从Django项目中的PostGre数据库查询。

Below is my code. 下面是我的代码。

flight_schedule_detail_instance = FlightScheduleDetail.objects.filter
            schedule_id=FlightSchedule.objects.filter(schedule_id=instance.schedule_id),
            flight_date=str(tomorrow_date)
            )

I need to filter all the instances which has a particular schedule_id and a flight_date . 我需要过滤所有具有特定schedule_idflight_date的实例。 This works in my localhost. 这适用于我的本地主机。 I am using sqlite in my localhost code. 我在本地主机代码中使用sqlite。 The server uses PostGre db. 服务器使用PostGre db。 How do I query to get my desired results? 如何查询以获得所需的结果?

Also, flight_date in my FlightScheduleDetail model is a DateField . 另外,我的FlightScheduleDetail模型中的flight_dateDateField And my schedule_id is a Foriegn key to another model which is a CharField in that model. 我的schedule_id是另一个模型的Foriegn键,该模型就是该模型中的CharField

I also tried without having a str wrapper to my tomorrow_date variable which is a date object. 我还尝试了没有对tomorrow_datedate变量str包装。

My models.py : 我的models.py

class FlightScheduleDetail(models.Model):
    flight_date = models.DateField(max_length=30, null=False, blank=False)
    schedule_id = models.ForeignKey(FlightSchedule, null=False, related_name="schedule_details", blank=False)
    route_id = models.CharField(max_length=150, null=False, blank=False, unique=True)
    flight_status = models.ForeignKey(Status, null=True, default=1)

    def __unicode__(self):
        return u'%s' % self.route_id

    class Meta:
        verbose_name_plural = "flights Schedule Details"


class FlightSchedule(models.Model):
    tail_number = models.ForeignKey(TailNumber, null=False, blank=False)
    schedule_id = models.CharField(max_length=40, null=False, blank=False, unique=True)
    flight_group_code = models.ForeignKey(FlightGroup, null=False, blank=False)
    minLength = MinLengthValidator(limit_value=7)
    binary = RegexValidator(r'^[0-1]*$', 'Only 0s and 1s are allowed.')
    scheduled_days = models.CharField(max_length=7, validators=[binary, minLength], null=True, blank=True,
                                      default="1111111")
    origin_port_code = models.ForeignKey(Port, null=False, related_name="Origin", blank=False)
    destination_port_code = models.ForeignKey(Port, null=False, related_name="Destination", blank=False)
    flight_departure_time = models.TimeField()
    start_date = models.DateField()
    end_date = models.DateField()

    def __unicode__(self):
        return u'%s' % self.schedule_id

    class Meta:
        verbose_name_plural = "flights Schedule"

and I am calculating my tomorrow_date as follows : 我正在计算我的tomorrow_date ,如下所示:

tomorrow_date = datetime.date.today() + datetime.timedelta(days=1)

This is a classic example of why you should use the same database in development and production. 这是一个为什么要在开发和生产中使用相同数据库的经典示例。 Sqlite does not have a concept of types. Sqlite没有类型的概念。 You can declare field types but they are not strictly followed. 您可以声明字段类型,但不严格遵循它们。 So you can easily insert text data into Date fields. 因此,您可以轻松地将文本数据插入“日期”字段。 Postgresql on the other hand is very strict about types. 另一方面,PostgreSQL对类型非常严格。

Updating my answer based on updates to the question. 根据问题的更新来更新我的答案。

I am not quite sure, if you need that elaborate query. 我不确定,是否需要该详细查询。 You have an instance of a FlightSchedule and that instance will give you direct access to all the related FilightScheduleDetail instances. 您有一个FlightSchedule实例,该实例将使您直接访问所有相关的FilightScheduleDetail实例。 you can just filter on it like this 你可以像这样过滤它

instance.flightscheduledetail_set.filter(flight_date=tommorow_date)

If you continue to have the error, it can only mean that there is a missig migration somewhere. 如果继续出现错误,则仅表示某处存在Missig迁移。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django.db.utils.ProgrammingError:运算符不存在:可变字符=整数 - django.db.utils.ProgrammingError: operator does not exist: character varying = integer django.db.utils.ProgrammingError:关系“...”不存在 - django.db.utils.ProgrammingError: relation “…” does not exist Django.db.utils.ProgrammingError:约束不存在 - Django.db.utils.ProgrammingError: constraint does not exist django.db.utils.ProgrammingError:关系“-”不存在 - django.db.utils.ProgrammingError: relation “ - ” does not exist django.db.utils.ProgrammingError: 列“问题”的类型是字符变化 [] 但默认表达式是整数类型 - django.db.utils.ProgrammingError: column “questions” is of type character varying[] but default expression is of type integer Django 迁移数据库 django.db.utils.ProgrammingError:关系“django_site”不存在 - Django Migrating DB django.db.utils.ProgrammingError: relation "django_site" does not exist Django 测试失败,出现“django.db.utils.ProgrammingError:关系“django_content_type”不存在” - Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist' django.db.utils.ProgrammingError: (1146 表不存在) - django.db.utils.ProgrammingError: (1146 table doesn't exist) Django Rest框架“ django.db.utils.ProgrammingError:关系“患者”不存在” - Django Rest Framework “django.db.utils.ProgrammingError: relation ”patient“ does not exist” django.db.utils.ProgrammingError:关系“ auth_user”不存在-django 2.0.2 - django.db.utils.ProgrammingError: relation “auth_user” does not exist - django 2.0.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM