# Create custom db table and fields from plugin

**URL:** https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164
**Category:** Development
**Created:** [November 30, 2015, 6:09pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164 "2015-11-30T18:09:49Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![deviousmachin3](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deviousmachin3](https://meta.discourse.org/u/deviousmachin3)
#### Post date: [November 30, 2015, 6:09pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/1 "2015-11-30T18:09:49Z")

</div>

Hi everyone, I’m working on developing a new plugin and I need to store some values in the database but I do not want to modify the discourse base system in any fashion. I’ve read through the beginners guide on plugin development and I did not see anything addressing this, essentially what i need is a table that has two fields that reference a single user that links another user or multiple users.

database → plugin\_users → column 1 would be user\_id and column 2 could be a comma separated list of user\_ids.

Ideally I would also be able to query this data to pull those values from an XHR request, as well as set those values via an xhr request, is this possible with the current discourse plugin model and if so how would one go about integrating this?

---

<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: [November 30, 2015, 6:14pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/2 "2015-11-30T18:14:23Z")

</div>

You might want to have a look at the [custom fields](https://github.com/discourse/discourse/blob/master/app/models/concerns/has_custom_fields.rb) (which are available for groups, users, categories, topics and posts) or the [plugin store](https://github.com/discourse/discourse/blob/master/app/models/plugin_store.rb) 😉

---

<div class="post-metadata">

### Author: ![deviousmachin3](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deviousmachin3](https://meta.discourse.org/u/deviousmachin3)
#### Post date: [November 30, 2015, 6:29pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/3 "2015-11-30T18:29:37Z")

</div>

Is there any supporting documentation on how to make use of these? For example is the custom field helper exposed through the api/ endpoint?

---

<div class="post-metadata">

### Author: ![deviousmachin3](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deviousmachin3](https://meta.discourse.org/u/deviousmachin3)
#### Post date: [November 30, 2015, 8:45pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/4 "2015-11-30T20:45:16Z")

</div>

Assuming I create a method within my plugin.rb file to set these values programmatically how could I access them through an xhr request?

Is there a way to expose internal plugin methods to the outside world?

For example something like

[//plugin.rb](https://plugin.rb) file contents  
def update\_fields  
//Save fields  
end

[//plugin.js](https://plugin.js) contents

```
            Discourse.ajax("plugin#update_fields", {
              type: 'POST',
              dataType: 'json',
              data: {#}
            }).then(function(json) { //On success
            
             }, function(json) { //On error

            });

```

---

<div class="post-metadata">

### Author: ![deviousmachin3](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@deviousmachin3](https://meta.discourse.org/u/deviousmachin3)
#### Post date: [November 30, 2015, 9:06pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/5 "2015-11-30T21:06:34Z")

</div>

Managed to expose these routes by mimicking an existing plugin and how they went about doing it, I’m not sure if there is documentation out there for these things but it doesn’t seem very easy to find from google searches etc.

Are there any plans in the near future to improve the documentation for these things? Thanks for pointing me in the right general direction, though those files provide little context on how to make use of those methods.

---

<div class="post-metadata">

### Author: ![Umer\_Mirza](https://avatars.discourse-cdn.com/v4/letter/u/4bbf92/32.png) [@Umer\_Mirza](https://meta.discourse.org/u/Umer_Mirza)
#### Post date: [May 27, 2017, 2:26pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/6 "2017-05-27T14:26:03Z")

</div>

hi! I am in exact neer of what you described in your post. I see that you were able to accomplish that by mimicking sone exisiting plugin? May I ask exactly which plugin did you copy? Can you please give some pointers?  
What I am trying to accomplish is to add some extra fields to the topics table in DB and then get/put data in those fields with REST api.

---

<div class="post-metadata">

### Author: ![raghav](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@raghav](https://meta.discourse.org/u/raghav)
#### Post date: [June 7, 2019, 5:48am UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/7 "2019-06-07T05:48:15Z")

</div>

@deviousmachin3 I find myself in a similar situation and I’d really appreciate it if you could share your solution to the problem you faced.

Thanks in advance! 😊

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [October 16, 2019, 9:48am UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/8 "2019-10-16T09:48:54Z")

</div>

This should answer the questions for folks here

> [@New columns in directory (/users)](https://meta.discourse.org/t/new-columns-in-directory-users/77814/5):
>
> FWIW I’ve decided to go this route with a plugin of mine, where PluginStoreRow and CustomFields didn’t qqquite cut it on their own. warning Caveat emptor warning You should be trying really hard not to do this - It’s not recommended behaviour to modify the database from within a plugin, but it can be done unobtrusively in small, additive doses (I’d say you never for any reason want to remove or change existing columns) Here’s how I did it: Write a migration in the plugin which modifies th…

---

<div class="post-metadata">

### Author: ![spirobel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/spirobel/32/170908_2.png) [@spirobel](https://meta.discourse.org/u/spirobel)
#### Post date: [October 16, 2019, 8:37pm UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/9 "2019-10-16T20:37:41Z")

</div>

I think its better to do migrations like the polls plugin. I created a migration and model generator for plugins: [GitHub - spirobel/discourse at plugin\_model\_and\_migrations](https://github.com/spirobel/discourse/tree/plugin_model_and_migrations) It puts the migration files at the same spot like in the polls plugin.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 24, 2023, 11:09am UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/10 "2023-06-24T11:09:52Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 24, 2023, 11:41am UTC](https://meta.discourse.org/t/create-custom-db-table-and-fields-from-plugin/36164/11 "2023-06-24T11:41:23Z")

</div>


