Domanda

How coalesce determine the return data type of the attribute?

USING

self.projects(
                select={"priority": "COALESCE(bm_rank, sales_rank, created_at)",

                },
                order_by=["priority"])

Its changing my float filed to the unicode string. I want to get float field in priority.

EDIT

bm_rank = models.FloatField("Bizman Rank", max_length=10, blank=True, null=True)    
sales_rank = models.FloatField("Sales Rank", max_length=10, blank=True, null=True)
created_at = models.DateTimeField("Date Submitted", auto_now_add=True) 
È stato utile?

Soluzione

What you are essentialy trying is to coalesce columns of different data types. You haven't specified your actual DB engine, but I can imagine only two sensible outcomes of such operation:

  • Throw error, which is not your case
  • Cast both columns' data types to common ancestor, if such ancestor exist, or to string type otherwise

Looks like in your case it's second option.

If you are trying to order by bm_rank then by sales_rank then by created_at you actually can do it without coalescing at all using order_by, like:

self.projects(order_by=["bm_rank", "sales_rank", "created_at"])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top