运算符不存在:字符变化+字符变化

时间:2019-11-24 06:25:50

标签: python django postgresql casting average

我将数据库从sqlite更改为postgresql以便为我的网站进行生产,但出现此错误。当我在sqlite本地工作时,没有得到此错误。使用Django。

ProgrammingError at /boards/1/companies/1/

operator does not exist: character varying + character varying
LINE 1: ...Col1, (AVG(((("boards_review"."move_in_condition" + "boards_...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Request Method:     GET
Request URL:    http://www.flythecoop.io/boards/1/companies/1/
Django Version:     2.2.6
Exception Type:     ProgrammingError
Exception Value:    

operator does not exist: character varying + character varying
LINE 1: ...Col1, (AVG(((("boards_review"."move_in_condition" + "boards_...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Exception Location:     /home/reviews/venv/lib/python3.8/site-packages/django/db/backends/utils.py in _execute, line 84
Python Executable:  /home/reviews/venv/bin/python3.8
Python Version:     3.8.0
Python Path:    

['/home/reviews/venv/bin',
 '/home/reviews/reviews',
 '/home/reviews/venv/lib/python38.zip',
 '/home/reviews/venv/lib/python3.8',
 '/home/reviews/venv/lib/python3.8/lib-dynload',
 '/usr/lib/python3.8',
 '/home/reviews/venv/lib/python3.8/site-packages']

Server time:    Sun, 24 Nov 2019 05:52:27 +0000
Error during template rendering

In template /home/reviews/reviews/templates/baseb.html, error at line 0
operator does not exist: character varying + character varying LINE 1: ...Col1, (AVG(((("boards_review"."move_in_condition" + "boards_... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 

看着错误,它说了一些有关代码片段的内容(AVG(((... 平均的作用是显示公司所有评论的平均评分。 每个字符串(例如“ move_in_condition”)都具有整数值。 我唯一的位置在我的模型中。py

class Company(models.Model):
    name = models.CharField(max_length=255, unique=True)
    #bio = models.TextField(max_length=4000)
    last_updated = models.DateTimeField(auto_now_add=True)
    board = models.ForeignKey(Board, on_delete = models.CASCADE, related_name='companies')
    starter = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='companies',
    )
    views = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.name

    def get_avg(self):
        return self.reviews.annotate(
            overall_rating = Avg(
                F('move_in_condition') + 
                F('treatment') + 
                F('response_speed') +
                F('maintenance_quality')
            )/4).aggregate(
                Avg('overall_rating'), 
                Avg('move_in_condition'),
                Avg('treatment'),
                Avg('response_speed'),
                Avg('maintenance_quality')
            )

查看其他响应,看来我必须添加显式的类型转换,但是我不确定如何实现,还是应该以某种方式完全摆脱“ +”?

编辑-如果那是更好的方法,则更新为使用integerfield,但仍然存在相同的问题。包括以下我的字段:

class Review(models.Model):
    RATING_CHOICES = (
        (1, '1'),
        (2, '2'),
        (3, '3'),
        (4, '4'),
        (5, '5'),
    )
    STAY = (
        ('less than 6 months', 'less than 6 months'),
        ('6 months', '6 months'),
        ('10 months', '10 months'),
        ('12 months', '12 months'),
        ('More than 1 year', 'More than 1 year'),
    )
    YES_NO = (
        ('Yes', 'Yes'),
        ('No', 'No'),
    )
    SECURITY = (
        ('100%', '100%'),
        ('75%', '75%'),
        ('50%', '50%'),
        ('25%', '25%'),
        ('0%', '0%'),
        ('Still waiting', 'Still waiting'),
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(null=True)
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE, related_name='reviews')
    updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete = models.CASCADE, null=True, related_name='+')
    company = models.ForeignKey(Company, on_delete = models.CASCADE, related_name='reviews')
    address = models.CharField(max_length=200, blank=False, default="")
    length_of_stay = models.CharField(max_length=20, choices=STAY, blank=False, default='None')

    move_in_condition = models.IntegerField(choices=RATING_CHOICES, blank=False, default='5')
    #Landlord Interaction
    treatment = models.IntegerField(choices=RATING_CHOICES, blank=False, default ="5")
    response_speed = models.IntegerField(choices=RATING_CHOICES, blank=False, default ="5")
    maintenance_quality = models.IntegerField(choices=RATING_CHOICES, blank=False, default ="5")    

    security_deposit_returned = models.CharField(max_length=5, choices=SECURITY, blank=False, default ="None")
    #put text "ignore if still waiting"
    is_this_a_fair_amount = models.CharField(max_length=5, choices=YES_NO, blank=False, default="1")
    would_you_recommend = models.CharField(max_length=5, choices=YES_NO, blank=False, default="1")
    additional_comments = models.TextField(max_length=4000)

    def __str__(self):
        return self.address

1 个答案:

答案 0 :(得分:2)

Postgres对string concat操作使用不同的(ANSI / SQL)符号。您应该使用<div class="row"> <ul class="col s12 collection" data-bind="foreach: Customers"> <li class="collection-item"> <span data-bind="text: name"></span> </li> </ul> </div> 代替||。像Postgres这样的运营商也拥有Oracle。

postgres=# SELECT varchar 'AHOJ' + varchar 'AHOJ';
ERROR:  operator does not exist: character varying + character varying
LINE 1: SELECT varchar 'AHOJ' + varchar 'AHOJ';
                              ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
postgres=# SELECT varchar 'AHOJ' || varchar 'AHOJ';
+----------+
| ?column? |
+----------+
| AHOJAHOJ |
+----------+
(1 row)