# Error: PG::UndefinedColumn. Custom field not creating column

**URL:** https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115
**Category:** Support
**Created:** [August 11, 2021, 9:01pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115 "2021-08-11T21:01:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JQ331](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@JQ331](https://meta.discourse.org/u/JQ331)
#### Post date: [August 11, 2021, 9:01pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115/1 "2021-08-11T21:01:54Z")

</div>

Do topic custom fields add a column to the topics model? I am getting an error saying no, and I’m trying to figure out what to do about it.

* * *

I have created a topic custom field in development, following [here](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485). Let’s call it `fun_level`. The custom field seems to be successfully created in development, bc if I do an ajax call to get back a topic I have created after adding the custom field–like `ajax("/t/112.json")`–it works: the topic data comes back showing the field `fun_level` with the value I entered.

But now I need to find a way to programmatically retrieve all topics with a certain value for the custom field (eg, get a list of all topics where `fun_level` = ‘super-duper-fun’).

I’ve considered doing an `ajax("/search")` for that, [but I haven’t gotten it to work yet.](https://meta.discourse.org/t/retrieve-topics-based-on-custom-field/199879/3)

Another method I thought of was to use classic rails: create a controller, and when I go to a certain path (/fun\_levels/:fun\_level), call the following show method:

```plaintext
  def show 
   respond_to do |format|
      format.json { render json: Topic.where(fun_level: 'super-duper-fun') }
    end
  end

```

Then, I call `ajax("/fun_levels/super-duper-fun.json")`…

This controller `show method/ajax call` strategy works if I am finding topics based on an id, title, etc. But when I use the above code-`Topic.where(fun_level: 'super-duper-fun)`–it sends an `internal server error`. And the logs say:

`ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column topics.fun_level does not exist LINE 1: ... "topics" WHERE "topics"."deleted_at" IS NULL AND "topics"."...`

Is there a way to get back topics based on a custom field using a controller method, like I’ve provided above? (like my: `{ render json: Topic.where(fun_level: 'super-duper-fun') }`)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 11, 2021, 9:56pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115/2 "2021-08-11T21:56:09Z")

</div>

There is a `TopicCustomField` model. You want that. Maybe have a look at [Topic List Previews (legacy)](https://meta.discourse.org/t/topic-list-previews/101646) or some other plugin that mucks with topics. Or maybe see HasCustomFields somewhere in discourse core.

---

<div class="post-metadata">

### Author: ![JQ331](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@JQ331](https://meta.discourse.org/u/JQ331)
#### Post date: [August 11, 2021, 10:19pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115/3 "2021-08-11T22:19:22Z")

</div>

Thanks–I was starting to figure something like that must be going on. So perhaps I’d need to do a join with that model (really having to dust off my rails here). I’ve tried something like this:

`Topic.joins(:topic_custom_field).where(fun_level: 'super-duper-fun') }`

but I get the error: `ActiveRecord::ConfigurationError (Can't join 'Topic' to association named 'topic_custom_field'; perhaps you misspelled it?)`

Any ideas on the right syntax?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 11, 2021, 10:25pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115/4 "2021-08-11T22:25:08Z")

</div>

I can’t find a TopicCustomField this instant (but I told you where to look! :wink)

Here’s this:

```plaintext
[4] pry(main)> CategoryCustomField.last
=> #<CategoryCustomField:0x000055be74a00bd0
 id: 2,
 category_id: 8,
 name: "enable_accepted_answers",
 value: "true",
 created_at: Mon, 26 Apr 2021 15:45:07.862617000 UTC +00:00,
 updated_at: Mon, 26 Apr 2021 15:45:07.862617000 UTC +00:00>

```

So something like

```plaintext
tcf=TopicCustomField.where(name: 'fun_level', value: 'super-duper-fun')

```

Then you can get the `topic_id` and such from `tcf` (it’s an array of all matches). If you know the `topic_id` you could do a `TopicCustomField.find_by(<all three things to make it unique>)`.

---

<div class="post-metadata">

### Author: ![JQ331](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@JQ331](https://meta.discourse.org/u/JQ331)
#### Post date: [August 11, 2021, 10:29pm UTC](https://meta.discourse.org/t/error-pg-undefinedcolumn-custom-field-not-creating-column/200115/5 "2021-08-11T22:29:45Z")

</div>

Cool. Thanks for the info. I’ll experiment with it.
