# Badge not being applied, sql error

**URL:** https://meta.discourse.org/t/badge-not-being-applied-sql-error/104267
**Category:** Bug
**Tags:** solved
**Created:** [December 14, 2018, 1:55pm UTC](https://meta.discourse.org/t/badge-not-being-applied-sql-error/104267 "2018-12-14T13:55:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lostdesign](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lostdesign](https://meta.discourse.org/u/lostdesign)
#### Post date: [December 14, 2018, 1:55pm UTC](https://meta.discourse.org/t/badge-not-being-applied-sql-error/104267/1 "2018-12-14T13:55:25Z")

</div>

edit: i now checked with “preview granted badges”, it now shows the following:

 ![screenshot](https://global.discourse-cdn.com/meta/original/3X/4/9/49443b84331c0e22340e2e84276e8013f7f0db60.png)

I don’t see it in my profile yet, when will it be added? What is the best trigger time for the badge?

Hi everyone,  
i just installed solved and added the following sql. After solving one thread, the badge was not applied.  
We tried running the sql query inside the [data explorer](https://meta.discourse.org/t/32566?silent=true). Here is the error i got in the [data explorer](https://meta.discourse.org/t/32566?silent=true):

```
ActiveRecord::PreparedStatementInvalid: missing value for :backfill in /*
 * DataExplorer Query
 * Query: /admin/plugins/explorer?id=3
 * Started by: lost
 */

```

It’s a ubuntu 18.04 w/ docker, fresh install from two days ago. I was using the following sql query i got form [Discourse Solved](https://meta.discourse.org/t/discourse-solved-accepted-answer-plugin/30155)

```
SELECT p.user_id, p.id post_id, p.updated_at granted_at
FROM badge_posts p
WHERE p.post_number > 1 AND 
    p.id IN (
      SELECT post_id FROM (
       SELECT pc.post_id, row_number() 
       OVER (PARTITION BY p1.user_id ORDER BY pc.created_at) as rnum
       FROM post_custom_fields pc
       JOIN badge_posts p1 ON p1.id = pc.post_id
       JOIN topics t1 ON p1.topic_id = t1.id
       WHERE name = 'is_accepted_answer' AND
                     value IS NOT NULL AND
                    p1.user_id <> t1.user_id AND 
        (
          :backfill OR 
           p1.user_id IN (
                   select user_id from posts where p1.id IN (:post_ids)
           )
       )
) X WHERE rnum = 1)

```

---

<div class="post-metadata">

### Author: ![lostdesign](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lostdesign](https://meta.discourse.org/u/lostdesign)
#### Post date: [December 14, 2018, 2:43pm UTC](https://meta.discourse.org/t/badge-not-being-applied-sql-error/104267/2 "2018-12-14T14:43:07Z")

</div>

Seems like we just got confused that “[data explorer](https://meta.discourse.org/t/32566?silent=true)” cannot run it like that and it just took quite long to actually hand out the badge.

I may ask here again:  
**What is the best trigger time for this?**
