<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="52741" data-post-id="52741">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Please keep in mind that you are talking with an noob that now learns the ropes so what I say may be wrong. I haven’t tried most of the options myself.</p>
<p>That being said I will try to create a simple project using <a href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/5" rel="nofollow">the link</a> tomconry posted and will post back. Maybe we can both learn something from this :D.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52741" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-52741" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52741"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="52754" data-post-id="52754">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello. Here is the example repo. <a href="https://github.com/voger/custom_id_test" class="inline-onebox" rel="noopener nofollow ugc">GitHub - voger/custom_id_test: example phoenix project for custom ids · GitHub</a><br>
It took me a while to make the pgsql function work <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> and I didn’t have much time to add controllers or example of custom id using changesets. This example demonstrates how to do it using the database with a sequence as in the link from tomconry. Take a look at the migration.</p>
<p>To use it after you clone it in your computer and do the migrations run iex -S mix and try</p>
<pre><code>iex(1)&gt; alias CustomIdTest.Context
CustomIdTest.Context
iex(2)&gt; Context.create_entry(%{entry: "Reandom text"})
[debug] QUERY OK db=28.5ms queue=0.2ms
INSERT INTO "entry_fragment" ("entry") VALUES ($1) ["Reandom text"]
{:ok,
 %CustomIdTest.EntryFragment{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "entry_fragment"&gt;,
  entry: "Reandom text", id: nil}}
iex(3)&gt; Context.get_entries
[debug] QUERY OK source="entry_fragment" db=4.6ms decode=18.1ms queue=0.2ms
SELECT e0."id", e0."entry" FROM "entry_fragment" AS e0 []
[%CustomIdTest.EntryFragment{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "entry_fragment"&gt;,
  entry: "random", id: 1616258530136822785},
 %CustomIdTest.EntryFragment{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "entry_fragment"&gt;,
  entry: "random", id: 1616258765361779714},
 %CustomIdTest.EntryFragment{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "entry_fragment"&gt;,
  entry: "Reandom text", id: 1616264079603667971}]
</code></pre>
<p><strong>Update:</strong><br>
<a class="mention" href="/u/acrolink" rel="nofollow">@acrolink</a><br>
I added another example on how to set the id from the changeset. This time I used a generator to speed up and you can see the results in a browser. I probably messed up naming and spelling because it was done in a hurry. I hope it doesn’t mind. <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>To use it go to <a href="http://localhost:4000/things_with_changeset" rel="noopener nofollow ugc">http://localhost:4000/things_with_changeset</a></p>
<p>Especially interesting I believe are [the migration] (<a href="https://github.com/voger/custom_id_test/blob/master/priv/repo/migrations/20171002055049_create_things_with_changeset.exs" class="inline-onebox" rel="noopener nofollow ugc">custom_id_test/priv/repo/migrations/20171002055049_create_things_with_changeset.exs at master · voger/custom_id_test · GitHub</a> ) and <a href="https://github.com/voger/custom_id_test/blob/master/lib/custom_id_test/custom_id_test/thing_wih_changeset.ex" rel="noopener nofollow ugc">the schema</a></p>
<p>I modified <a href="https://github.com/voger/custom_id_test/blob/master/lib/custom_id_test_web/controllers/thing_wih_changeset_controller.ex" rel="noopener nofollow ugc">the controller</a> with this line <code>alias CustomIdTest.CustomIdTest, as: CIT</code> and renamed the relevant function calls. This is irrelevant  to the problem at hand. I only did this because I messed up the naming. You don’t have to do this in your controllers.</p>
<p>It is not a well thought example but I hope it helps.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52754" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-52754" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52754"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="52793" data-post-id="52793">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="acrolink" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/120/8985_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  acrolink
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/voger" rel="nofollow">@voger</a></p>
<p>Thank you very very much for the efforts put into this. I am new to Phoenix and pretty impressed by the speed it offers. Yet, I see that the documentation can be sometimes not clear and there is currently small amount of contributed libraries (elixir gems <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> or whatever they are called) and rather few HOWTO articles. Your code for sure makes a difference.</p>
<p>When it comes to doing it by PostgreSQL, I think your solution should work fine for most uses but think if entities will be created in bulk (e.g. 100 at a time), there is a possibility that the auto generate procedure would produce identical ids.</p>
<p>The changeset method is fine. I have already tried it. However, it sets the value already when the form is loaded. In a real system (with bulk insertions are made by custom controller calls or by Angualar UI with a JSON API backend) that should not be a problem I guess (the id should be generated at the time of insertion only, not before).</p>
<p>So, you think if <code>@primary_key {:id, :id, autogenerate: false}</code> is set then the <code>id</code> can be set inside the <code>params</code> object or does it get only set by the changeset function upon its initialization?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52793" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-52793" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52793"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="52817" data-post-id="52817">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tomconroy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomconroy/120/6323_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tomconroy
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="acrolink" data-post="14" data-topic="9019">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/48/8985_2.png" class="avatar"> acrolink:</div>
<blockquote>
<p>I think your solution should work fine for most uses but think if entities will be created in bulk (e.g. 100 at a time), there is a possibility that the auto generate procedure would produce identical ids.</p>
</blockquote>
</aside>
<p>This function originally comes from this instagram article:</p>
<p><a href="https://engineering.instagram.com/sharding-ids-at-instagram-1cf5a71e5a5c" class="onebox" target="_blank" rel="noopener nofollow ugc">https://engineering.instagram.com/sharding-ids-at-instagram-1cf5a71e5a5c</a></p>
<blockquote>
<p>we can generate 1024 IDs, per shard, per millisecond</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52817" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-52817" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52817"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="52832" data-post-id="52832">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="acrolink" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/120/8985_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  acrolink
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/voger" rel="nofollow">@voger</a></p>
<p>I have just got time only now to give your code the attention and implementation it deserves <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> Thank you very much, more details later.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52832" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-52832" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52832"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="52845" data-post-id="52845">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/acrolink" rel="nofollow">@acrolink</a></p>
<p>I am glad you found my examples helpful</p>
<blockquote>
<p>When it comes to doing it by PostgreSQL, I think your solution should work fine for most uses but think if entities will be created in bulk (e.g. 100 at a time), there is a possibility that the auto generate procedure would produce identical ids.</p>
</blockquote>
<p>These are not solutions. The SQL example is just that. An example how you can apply a SQL function in your migration. The example I used is just a copy paste from the link so I can have some function to showcase. I didn’t even took the time to understand what it does :D. You probably want to use your own implementation.</p>
<blockquote>
<p>The changeset method is fine. I have already tried it. However, it sets the value already when the form is loaded. In a real system (with bulk insertions are made by custom controller calls or by Angualar UI with a JSON API backend) that should not be a problem I guess (the id should be generated at the time of insertion only, not before).</p>
</blockquote>
<p>Well, indeed it generates an ID when the form is created but it replaces it with a new ID when the form is submitted. The current behavior is not optimal so you could either create two changesets (one for new and one for create)</p>
<pre><code>  @doc false
  def changeset(%ThingWihChangeset{} = thing_wih_changeset, attrs) do
    thing_wih_changeset
    |&gt; cast(attrs, [:entry])
    |&gt; validate_required([:entry])
  end

  def create_changeset(%ThingWihChangeset{} = thing_wih_changeset, attrs) do
    thing_wih_changeset
    |&gt; changeset(attrs)
    |&gt; put_change(:id, get_current_unix_time())
  end 
</code></pre>
<p>and then in <code>lib/custom_id_test/custom_id_test/thing_wih_changeset.ex</code> the create function becomes</p>
<pre><code>  def create_thing_wih_changeset(attrs \\ %{}) do
    %ThingWihChangeset{}
    |&gt; ThingWihChangeset.create_changeset(attrs)
    |&gt; Repo.insert()
  end
</code></pre>
<p>or alternatively you can remove completely the put_change from the changeset and use it in your create function</p>
<pre><code> def create_thing_wih_changeset(attrs \\ %{}) do
    %ThingWihChangeset{}
    |&gt; ThingWihChangeset.changeset(attrs)
    |&gt; Ecto.Changeset.put_change(:id, get_current_unix_time())
    |&gt; Repo.insert()
  end

  defp get_current_unix_time do
    DateTime.utc_now() |&gt; DateTime.to_unix()
  end
</code></pre>
<p>Also for the <code>:autogenerate</code> option I went after the documentation. <a href="https://hexdocs.pm/ecto/Ecto.Schema.html#module-primary-keys" class="inline-onebox" rel="noopener nofollow ugc">Ecto.Schema — Ecto v3.14.0</a> where it says that if set to <code>true</code> the database is responsible for setting it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52845" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-52845" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52845"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="52917" data-post-id="52917">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="acrolink" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/120/8985_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  acrolink
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="voger" data-post="17" data-topic="9019">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/48/3322_2.png" class="avatar"> voger:</div>
<blockquote>
<p>Well, indeed it generates an ID when the form is created but it replaces it with a new ID when the form is submitted. The current behavior is not optimal so you could either create two changesets (one for new and one for create)</p>
</blockquote>
</aside>
<p>It is fine since what is important is that the time-stamp that gets stored in the database reflects time at save. Using <code>put_change</code> inside the create method is what I was originally looking for (thanks for setting me on that direction, in Phoenix 1.3 this function is found inside the context definition, I failed to find it before).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="52917" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-52917" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="52917"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="57594" data-post-id="57594">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="acrolink" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/acrolink/120/8985_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  acrolink
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/voger" rel="nofollow">@voger</a></p>
<p>When defining the custom <code>id</code> field in the schema, do you know how to set the column type as <code>BIGINT</code> (postgres)? In the migration itself? And how to define it as such in the schema file? Thank you.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="57594" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-57594" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="57594"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="57600" data-post-id="57600">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="voger" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/voger/120/3322_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  voger
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hello. I don’t have access to elixir right now so I can’t test what I say is accurate. Anyway here is my attempt</p>
<p>If you need autoincrementing bigint you may <strong>not</strong> need to do anything. [According to the docs]<br>
(<a href="https://hexdocs.pm/ecto/Ecto.Migration.html#content" rel="noopener nofollow ugc">https://hexdocs.pm/ecto/Ecto.Migration.html#content</a>)</p>
<blockquote>
<p>:migration_primary_key - Ecto uses the :id column with type :bigserial but you can configure it via:<br>
config :app, App.Repo, migration_primary_key: [id: :uuid, type: :binary_id]</p>
</blockquote>
<p>So it seems Ecto already uses an autoincrementing bigint as an <code>:id</code>. <a href="https://www.postgresql.org/docs/9.1/static/datatype-numeric.html" rel="noopener nofollow ugc">Not the full range. Only the positive values</a></p>
<p>If for some reason you don’t want autoincrementing values then you do this in your schema</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"> def change do

    create table(:books, primary_key: false) do
      add :id, :bigint, primary_key: true
      add :title, :string

      timestamps()
    end
</code></pre>
<p>but now you have to handle the key yourself.</p>
<p>Again, I can’t test the accuracy of the above.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="57600" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-set-id-value-in-code-upon-entity-creation/9019/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-57600" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="57600"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>