# "Sorry, An error has occurred" when changing a badge sql trigger to "When a user is edited or created"

**URL:** https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061
**Category:** Bug
**Created:** [May 21, 2015, 7:46am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061 "2015-05-21T07:46:32Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 7:46am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/1 "2015-05-21T07:46:32Z")

</div>

I’ve created a new badge which is assigned when a user fill out a custom field. Unfortunately if I set the SQL to trigger when they edit their profile, I get this pop-up. Setting it to query daily works.

I checked for error logs but there seems to be nothing there.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [May 21, 2015, 8:11am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/2 "2015-05-21T08:11:13Z")

</div>

More missing error text. The actual error will show up in your network inspector.

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 8:23am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/3 "2015-05-21T08:23:53Z")

</div>

Sorry, but where is the network inspector?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [May 21, 2015, 8:49am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/4 "2015-05-21T08:49:05Z")

</div>

The one in your browser

![](https://global.discourse-cdn.com/meta/original/3X/9/a/9ae2f82ad7ea11036face3c2e4ce7bff9007bc50.png)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [May 21, 2015, 8:56am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/5 "2015-05-21T08:56:45Z")

</div>

Yeah missing error text is an Ember upgrade regression cc @eviltrout.

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 9:09am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/6 "2015-05-21T09:09:42Z")

</div>

Is this useful?

 ![](https://global.discourse-cdn.com/meta/original/4X/a/a/1/aa115254ad0f1080a8f2b55193d2c67a9a867af8.png)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [May 21, 2015, 9:52am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/7 "2015-05-21T09:52:27Z")

</div>

Yep… so here’s how it works. When a user updates their profile, your query is run with the `user_ids` parameter set to a comma-separated list of all the users who updated your profile, and you’re supposed to filter your query so that only badges/rows for those users are returned/checked.

Your query is _also_ run daily, with the :user\_ids parameter unset and `:backfill` set to true.

The implementation looks like this:

```sql
SELECT u.id user_id, current_timestamp granted_at
FROM ..., users u
WHERE ...
AND ...
AND (
  u.id IN (:user_ids)
  OR :backfill
)
AND ...

```

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 10:13am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/8 "2015-05-21T10:13:40Z")

</div>

OK so I have this query:

```
SELECT cf.user_id user_id, cf.updated_at granted_at
FROM user_custom_fields cf
WHERE cf.name like 'user_field_1' AND
LENGTH(cf.value) > 1

```

would it work if I modify it to:

```
SELECT cf.user_id user_id u.id, cf.updated_at granted_at
FROM user_custom_fields cf u
WHERE cf.name like 'user_field_1'
AND (
  u.id IN (:user_ids)
  OR :backfill
)
AND LENGTH(cf.value) > 1

```

?

(Sorry I’m not very good with SQL)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [May 21, 2015, 10:16am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/9 "2015-05-21T10:16:37Z")

</div>

Almost.

```plaintext
    SELECT cf.user_id user_id, cf.updated_at granted_at
    FROM user_custom_fields cf u
    WHERE cf.name = 'user_field_1'
    AND (
      cf.user_id IN (:user_ids)
      OR :backfill
    )
    AND length(cf.value) > 1

```

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 10:17am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/10 "2015-05-21T10:17:58Z")

</div>

Awesome, thanks a lot!

EDIT: Oh, it’s telling me there’s an error with the query when trying to run a preview…

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 10:20am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/11 "2015-05-21T10:20:47Z")

</div>

This seems to work (I removed the ‘u’ table from the FROM)

```
SELECT cf.user_id user_id, cf.updated_at granted_at
FROM user_custom_fields cf
WHERE cf.name = 'user_field_2'
AND (
  cf.user_id IN (:user_ids)
  OR :backfill
)
AND length(cf.value) > 1

```

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [May 21, 2015, 10:39am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/12 "2015-05-21T10:39:57Z")

</div>

Oops, didn’t spot that you added that. Yeah, it’s `FROM TABLENAME ALIAS [, TABLENAME ALIAS]...`

---

<div class="post-metadata">

### Author: ![db0](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/db0/32/116141_2.png) [@db0](https://meta.discourse.org/u/db0)
#### Post date: [May 21, 2015, 11:12am UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/13 "2015-05-21T11:12:36Z")

</div>

Great, I’ll post this in the thread where I found the code to use by others

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [May 21, 2015, 8:19pm UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/14 "2015-05-21T20:19:57Z")

</div>

Fixed here:

[https://github.com/discourse/discourse/commit/381784ea62ded426d268678b01226d49650800c0](https://github.com/discourse/discourse/commit/381784ea62ded426d268678b01226d49650800c0)

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [May 21, 2015, 9:12pm UTC](https://meta.discourse.org/t/sorry-an-error-has-occurred-when-changing-a-badge-sql-trigger-to-when-a-user-is-edited-or-created/29061/15 "2015-05-21T21:12:41Z")

</div>


