# File for modifying the SQL query for create 'post'?

**URL:** https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173
**Category:** Development
**Tags:** rest-api
**Created:** [September 16, 2022, 7:14am UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173 "2022-09-16T07:14:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![charnelpezai](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/charnelpezai/32/273284_2.png) [@charnelpezai](https://meta.discourse.org/u/charnelpezai)
#### Post date: [September 16, 2022, 7:14am UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/1 "2022-09-16T07:14:43Z")

</div>

Hi guys, to give some background, I’m trying to add another column to the table schema of `posts` table, I want to add another column which is `meta_tag_id`. I got to a point where I can pass the data that I want to store in the parameters of `PostController`, check the log below for reference(this is from the logs of `d/rails s`):

```plaintext
Started POST "/posts" for 127.0.0.1 at 2022-09-16 06:08:19 +0000
Processing by PostsController#create as */*
  Parameters: {"raw"=>"Another dummy reply, for testing purposes.", "unlist_topic"=>"false", "category"=>"5", "topic_id"=>"23", "is_warning"=>"false", "archetype"=>"regular", "typing_duration_msecs"=>"4500", "composer_open_duration_msecs"=>"11534", "featured_link"=>"", "shared_draft"=>"false", "draft_key"=>"topic_23", "meta_tag_id"=>"summary", "nested_post"=>"true"}

```

Now, what I want to achieve is to insert the `meta_tag_id` parameter ☝ in another column of the `posts` table, for reference, here is a sample logs of the SQL query when inserting a record in my `posts` table:

```plaintext
Post Create (1.1ms) INSERT INTO "posts" ("user_id", "topic_id", "post_number", "raw", "cooked", "created_at", "updated_at", "sort_order", "last_editor_id", "last_version_at", "word_count", "baked_at", "baked_version") VALUES (1, 23, 7, 'Another dummy reply, for testing purposes.', '<p>Another dummy reply, for testing purposes.</p>', '2022-09-16 06:08:22.283658', '2022-09-16 06:08:22.283658', 7, 1, '2022-09-16 06:08:22.293786', 6, '2022-09-16 06:08:22.283601', 2) RETURNING "id"

```

I would be grateful if you guys could point me in the right direction. I’m not familiar with ruby, so I’m having a hard time finding the find that I should modify to achieve the adding of a new column and incorporating the `meta_tag_id` variable to the values of the query. Thank you in advance guys! 😄

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 16, 2022, 7:27am UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/2 "2022-09-16T07:27:12Z")

</div>

Modifying such a key table directly is probably risky, consider instead using the `PostCustomField` model, it exists for exactly this kind of purpose: to attach bespoke custom data to Posts, usually in a plugin.

> <https://github.com/discourse/discourse/blob/main/app/models/post_custom_field.rb>

If you have performance issues then maybe you can revisit, but this is a much simpler and safer solution.

---

<div class="post-metadata">

### Author: ![charnelpezai](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/charnelpezai/32/273284_2.png) [@charnelpezai](https://meta.discourse.org/u/charnelpezai)
#### Post date: [September 16, 2022, 12:45pm UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/3 "2022-09-16T12:45:39Z")

</div>

Thank you for the reply, okay so my `meta_tag_id` will be stored in the `post_custom_field` table by using the `PostCustomField` class? Then to access the stored data on the post stream, I should get the data of the `post_custom_field` table also and incorporate the `meta_tag_ids` to the posts? Am I getting the correct use case of the `PostCustomField` class?

---

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [September 16, 2022, 12:49pm UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/4 "2022-09-16T12:49:29Z")

</div>

See [How to add custom fields to models](https://meta.discourse.org/t/how-to-add-custom-fields-to-models/184485)

There is no example for Posts but there are examples for Topics and Categories and they work the same way.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 16, 2022, 12:56pm UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/5 "2022-09-16T12:56:09Z")

</div>

Here are examples of their use:

> <https://github.com/discourse/discourse-assign/blob/7e85101b89e298b14e1cef50c37ee3de8ba63327/plugin.rb#L835>

and

> <https://github.com/VinkasHQ/discourse-autobot/blob/5777c6878d1115c3233ce79701cb4a8c35f61742/lib/autobot/post_creator.rb#L111>

Make sure you are familiar with [Rails Active Record Model](https://guides.rubyonrails.org/active_model_basics.html) concepts, because you’d normally want to do everything via Ruby on Rails rather than directly on the database.

---

<div class="post-metadata">

### Author: ![charnelpezai](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/charnelpezai/32/273284_2.png) [@charnelpezai](https://meta.discourse.org/u/charnelpezai)
#### Post date: [September 16, 2022, 1:12pm UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/6 "2022-09-16T13:12:38Z")

</div>

Thanks for the replies, I will look at the references and examples that you guys given. Oh, I’m not familiar with active model concept, or in ruby in general, yet. This is very helpful, thanks again guys. 🙏

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [September 16, 2022, 1:27pm UTC](https://meta.discourse.org/t/file-for-modifying-the-sql-query-for-create-post/239173/7 "2022-09-16T13:27:20Z")

</div>

It’s Lego, for databases 🧱
