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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I just want to say that I really enjoyed this discussion. The argument that “embeds has_many should behave as closely as possible to a normal has_many” is very strong and was enough to make me re-think my data model decision.</p>
<p>That said, the reality is <code>nil</code> and <code>empty list</code> are two different things, conceptually. Imagine a scenario where you want to model out an <strong>optional</strong> white list of some sort.</p>
<ul>
<li><code>nil</code> would mean “I’m not using the whitelist; allow everyone in”</li>
<li><code>[]</code> would mean, I don’t want to let anyone in (no one is on the list)</li>
<li><code>["foo"]</code> would mean, “I only want “foo” to be allowed in”</li>
</ul>
<p>I am now seeing that you could <strong>only</strong> have this data model w/ an embedded structure of somewhere since, as this thread points out, a <code>has_many</code> has no <code>null</code> concept; you’d have to add a second field <code>is_using_whitelist</code> (which avoids a “magic <code>nil</code>” which, to be fair, is a smell w/ what I was hoping to do.)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="257714" 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/why-does-embeds-many-enforce-a-default-of/48704/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-257714" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="257714"
                     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="257818" data-post-id="257818">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="krstfk" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  krstfk
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m not gonna die on that hill, but -to me- the data you’re suggesting is something like Maybe[OkList]. So you basically have two inhabitants of your type: Some(maybe_empty_list) and None (null).<br>
Personally I’d model that as an embed one.<br>
Again to me embeds_many suggests a list and I’m fine with having either an empty list or a list. I’m a bit dense, so I don’t really know what null would be in that list context.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="257818" 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/why-does-embeds-many-enforce-a-default-of/48704/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-257818" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="257818"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="257821" data-post-id="257821">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh I love that.. so the data model would be like</p>
<p>record.whitelist → <code>nil                 # whitelist is nil; allow all ids</code><br>
record.whitelist → <code>{ids: ["foo"]} # white list has 1 id; only allow it</code><br>
record.whitelist → <code>{ids: []} # whitelist has no ids, don't allow anyone</code></p>
<p>I like that a lot, actually (provided you <strong>have</strong> to accomplish the optional whitelist feature)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="257821" 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/why-does-embeds-many-enforce-a-default-of/48704/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-257821" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="257821"
                     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="292141" data-post-id="292141">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cantiero" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cantiero
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I just stumbled upon this problem in another situation.</p>
<p>I’m incorporating a new field into a system that has been operational for a substantial period. The system requires users to upload sets of data, after which they conduct a series of five intensive computations on data subsets in a prescribed sequence, interspersed with manual adjustments. The new field we’re adding will track the most recent ‘n’ calculations performed on each subset, providing users with a historical context. Moreover, it will determine the last registered step to inhibit calculations not appropriate for the current state.</p>
<p>A complication arises due to the inability to ascertain the calculations already made for the subsets created before the inclusion of this new field. Consequently, we need to implement a strategy to ensure a seamless transition:</p>
<ul>
<li>nil: for pre-existing subsets during migration, the default value is used, and any calculation is permitted.</li>
<li><span class="chcklst-box fa fa-square-o"></span>: for new subsets post-migration, the default value permits only the first calculation.</li>
<li>[%{…}, %{…}]: retrieves the last calculation from the head and responds accordingly.</li>
</ul>
<p>Hence, in our scenario, the absence of data is significant, and an empty list does not adequately convey this.</p>
<p>I believe that similar to a number that could be 0 or null, or a string that might be “” or null, an array should also be permitted to be <span class="chcklst-box fa fa-square-o"></span> or null.</p>
<p>Null is neither a number, a string, nor a list. Rather, it represents the lack of information in a field. And I think that all (or most) databases, such as Postgres, allow null in array fields for this very reason.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="292141" 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/why-does-embeds-many-enforce-a-default-of/48704/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-292141" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="292141"
                     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="323272" data-post-id="323272">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I had a similar situation, except that my valid types were <code>null</code> and <code>[%{…}, ...]</code> (no empty lists allowed). The solution that I’ve come up with is to implement an <code>Ecto.Type</code> instead of relying on <code>embeds_many</code>, which is a lot of work for this particular corner case.</p>
<p>My sample workaround is here:</p>
<aside class="quote quote-modified" data-post="9" data-topic="62698">
  <div class="title">
    <div class="quote-controls"></div>
    <img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/halostatue/48/2389_2.png" class="avatar">
    <div class="quote-title__text-content">
      <a href="https://forum.elixirforum.com/t/clearing-an-embeds-many-entry-cast-embed-3/62698/9" rel="nofollow">Clearing an `embeds_many` entry (`cast_embed/3`)</a> <a class="badge-category__wrapper " href="/c/questions-help/questions/53" rel="nofollow"><span data-category-id="53" style="--category-badge-color: #C14BFB; --category-badge-text-color: #000000; --parent-category-badge-color: #C14BFB;" data-parent-category-id="171" data-drop-close="true" class="badge-category --style-square --has-parent" title="Elixir Questions / Help"><span class="badge-category__name">Questions</span></span></a>
    </div>
  </div>
  <blockquote>
    One workaround is to define an Ecto.Type. My example here isn’t well organized (the type here should be a different module, OptionList), but that’s fine for an example: 
defmodule Test.Flow do
  use Ecto.Schema
  import Ecto.Changeset

  defmodule Option do
    use Ecto.Schema
    use Ecto.Type

    import Ecto.Changeset

    @primary_key false
    embedded_schema do
      field :value, :string
    end

    def changeset(opts \\ %__MODULE__{}, attrs) do
      cast(opts, attrs, [:value])
    end
…
  </blockquote>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="323272" 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/why-does-embeds-many-enforce-a-default-of/48704/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-323272" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="323272"
                     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="368749" data-post-id="368749">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I can understand the reasoning, but I just had a case where being able to set an embeds_many to <code>nil</code> would have saved me a lot of trouble.</p>
<p>I have an existing schema/table with a <code>field :foo, {:array, :string}</code>. This field contains encoded values (I know, not the best design, but thats just how the database was designed quiet some time ago). I need to migrate to a new <code>embeds_many :foo_new, MyCustomStruct</code>. The table is rather large, so I can’t migrate all of the data at once and instead have to keep both columns running simultaneously for a while, i.e. the migration happens in the background.</p>
<p>Now in an API endpoint, I have to decide which of the two fields is the one, that I have to read the data from and send it to the client. Initially I wanted to allow <code>nil</code> for the <code>:foo_new</code> embed, so I could use it as a marker that tells me whether the record has already been migrated from <code>:foo</code> to <code>:foo_new</code>. This would have worked well, because <code>[]</code> is a valid value and I can’t use it to decide whether the data has been migrated. To be more specific, the code could have ignored the old <code>:foo</code> field and just write the <code>:foo_new</code> field, so as soon as there is a value in there, I’d have known, that the migration logic did run and <code>:foo_new</code> is the new source of truth.</p>
<p>It even works in production, because the database allows <code>NULL</code> values and Ecto returns them as <code>nil</code> for the <code>embeds_many</code>. However, things start to get problematic as soon as I write tests, because then I have to write a lot of workarounds to get the <code>nil</code> values into the test database, effectively rendering ExMachina etc. useless, because the thousands of existing tests now insert <code>foo: [...], foo_new: []</code> and I can’t opt out of that behavior.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="368749" 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/why-does-embeds-many-enforce-a-default-of/48704/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-368749" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="368749"
                     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>