Repository: RocketMap/RocketMap

Issue:
Heatmap not working with MySql version 5.7.13
<!--
  
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!
  ISSUES ARE NOT FOR SUPPORT!!!

  Join discord (channel: #help) for support at this link: https://discord.gg/PWp2bAm

-->

<!--- Provide a general summary of the issue in the Title above -->

After commit for pulling  #1103 the heatmap is not shown because mysql 5.7.13 complain about only full-group-by which is enabled by default
Error thrown:
`InternalError: (1055, u"Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'pokemon_go.t1.latitude' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")
`
## Expected Behavior

<!--- If you're describing a bug, tell us what should happen -->

<!--- If you're suggesting a change/improvement, tell us how it should work -->

Heatmap should show for spawned pokemon in full stats
## Current Behavior

<!--- If describing a bug, tell us what happens instead of the expected behavior -->

<!--- If suggesting a change/improvement, explain the difference from current behavior -->

Heatmap is not shown
## Possible Solution

<!--- Not obligatory, but suggest a fix/reason for the bug, -->

<!--- or ideas how to implement the addition or change -->

The most elegant solution is to rewrite such query, a workaround would be disable only-full-group-by for the database but is not suggested
## Steps to Reproduce (for bugs)

<!--- Provide a link to a live example, or an unambiguous set of steps to -->

<!--- reproduce this bug. Include code to reproduce, if relevant -->

1.Install PokemonGo-Map commit e92fa833745e301af88eb8e1f5e592c7d3283291
2.Configure it to use mysql 5.7.x
3.Open heatmap for a pokemon
## Context

<!--- How has this issue affected you? What are you trying to accomplish? -->

<!--- Providing context helps us come up with a solution that is most useful in the real world -->
## Your Environment

<!--- Include as many relevant details about the environment you experienced the bug in -->
- Version used: Commit e92fa833745e301af88eb8e1f5e592c7d3283291
- Environment name and version (e.g. Python 2.7): Python 2.7 mysql 5.7.13
- Operating System and version (desktop or mobile): ubuntu server 16.04



Discussion:
Will create  a fix today 

As hotfix one could add the following lines to mysql config file e.g. /etc/mysql/mysql.conf.d/mysqld.cnf 

```
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
```

and restart mysql: `service mysql restart to restart MySQL`

source: http://craftcms.stackexchange.com/a/13662 

@Cliffield that would disable the feature for all databases managed by mysql! I think the best option is to just rewrite the query.

https://github.com/PokemonGoMap/PokemonGo-Map/issues/1205 here a fix for this issue, you can test it. Should work with full-group-by 

Confirmed #1205 fixes heatmaps with ONLY_FULL_GROUP_BY enabled.

how the hell did i do that

i didn't even click in the issue

oh wait i see

1205 did it cool closed

@beccasafan How did you tested? i just pulled last commit and the error is still there!

Edit: the error seemed the same but is different  
`InternalError: (1055, u"Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'pokemon_go.t1.disappear_time' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")`

The problem now is in the order by clause

EDIT2: The query is `query = (Pokemon
                 .select(Pokemon.latitude, Pokemon.longitude, Pokemon.pokemon_id, fn.Count(Pokemon.spawnpoint_id).alias('count'), Pokemon.spawnpoint_id)
                 .where((Pokemon.pokemon_id == pokemon_id) &
                        (Pokemon.disappear_time > timediff)
                        )
                 .order_by(Pokemon.disappear_time.asc())
                 .group_by(Pokemon.latitude, Pokemon.longitude, Pokemon.pokemon_id, Pokemon.spawnpoint_id)
                 .dicts()
                 )`

Why we have and order by clause? For the heatmap it shouldn't be relevant. I cannot test it but if this query is not used for anything else removing the order by now should do the job. 
Can someone test this?


Resolving patch:
diff --git a/pogom/models.py b/pogom/models.py
--- a/pogom/models.py
+++ b/pogom/models.py
@@ -214,7 +214,6 @@ def get_appearances(cls, pokemon_id, timediff):
                  .where((Pokemon.pokemon_id == pokemon_id) &
                         (Pokemon.disappear_time > timediff)
                         )
-                 .order_by(Pokemon.disappear_time.asc())
                  .group_by(Pokemon.latitude, Pokemon.longitude, Pokemon.pokemon_id, Pokemon.spawnpoint_id)
                  .dicts()
                  )


Test patch: