Body: <p>When I am creating a new field (and I've tried plain text, redactor, supertable and matrix field) and associating it with an existing entry type (using the Control Panel to do so), I run into an issue where I can't load data to the field  with a migration.</p>
  
  <p>Here's code from the migration I might try to run to put some data in this field (in this case, it is a redactor).</p>
  
  <pre><code>  foreach ($attorneys as $attorney) { // iterate over json data we are going to import.
      $craft_attorney_id = // lookup attorney id
      $entry = Entry::find()-&gt;id($craft_attorney_id)-&gt;status(['enabled','disabled'])-&gt;one();
      if (!$entry) {
        continue;
      }
      $accolades = 'test text';
      $entry-&gt;setFieldValues(array('accolades' =&gt; $accolades));
      if(!\Craft::$app-&gt;elements-&gt;saveElement($entry)) {
          throw new \Exception("Couldn't save entry: " . print_r($entry-&gt;getErrors(), true));
      }
    }
  </code></pre>
  
  <p>When I view one entry in the CP and enter data in the new field, and then rerun the migration (take it down then up), the new data from the migration is saved just fine (for that element. but not for any others).</p>
  
  <p>I've set the redactor field to 'full html', and also stripped the tags from the field to be inserted.</p>
  
  <p>Our content table has 56 columns, but many are text, so I don't think I'm running into an issue with row length ( <a href="https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html#innodb-maximums-minimums" rel="nofollow noreferrer">https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html#innodb-maximums-minimums</a> )</p>
  
  <p>Tried clearing all the caches.</p>
  
  <p>Tried upgrading from 3.0.9 to 3.0.27.</p>
  
  <p>Also tried to refresh fields on the fields service:       <code>\Craft::$app-&gt;fields-&gt;refreshFields();</code> at the beginning of the migraiton.</p>
  
  <p>If I try to read from the entry in the same migration, I get this exception</p>
  
  <pre><code>Invalid field handle: accolades (/.../vendor/craftcms/cms/src/base/Element.php:1932)
  #0 /../vendor/craftcms/cms/src/base/Element.php(1612): craft\base\Element-&gt;normalizeFieldValue('accolades')
  #1 /../migrations/m181016_224151_import_accolades_v2.php(59): craft\base\Element-&gt;getFieldValue('accolades')
  #2 /../vendor/craftcms/cms/src/db/Migration.php(56): craft\contentmigrations\m181016_224151_import_accolades_v2-&gt;safeUp()
  </code></pre>
  
  <p>Except I can find that field using the fieldService. Maybe it is something to do with the normalization?</p>
  
  <p>Am I saving the entry incorrectly? Do I need to do some kind of special call to refresh the element mapping of fields?</p>
  
  <p>Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-21T19:50:11.120Z
Comments.Text: Could you please explain the first two lines of your code? I'm not sure if you have a creative way to fetch your entries of if you don't know what's happening exactly
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-22T14:32:17.213Z
Comments.Text: Hi @RobinSchambach, sorry, I should have explained that. I'm just pulling from a json file (previously parsed) and looking up the existing entry id by the name that is pulled from the json file. I will update the code sample.
Comments.UserDisplayName: mooreds
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-22T14:47:08.057Z
Comments.Text: The "problem" I see is: if it doesn't find a valid id the value might be `null` or `0` and if that's the case Craft will ignore the parameter and instead of returning `null` as well it will return the first found Entry that might not have the field in it's layout. Can you really make sure every title is 100% bound to an entry id?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-23T17:31:19.033Z
Comments.Text: If we don't see an entry, we `continue`. At least that is the intention, so that we always have a valid entry to associate the data with. I thought the `if (!$entry) {` line did that. Or am I missing your point?
Comments.UserDisplayName: mooreds
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-23T17:40:24.847Z
Comments.Text: My point is you should check if your id is valid as well. If id is null Craft will return the first entry it can find that might have a different layout because it can be in a different section
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-24T03:22:19.477Z
Comments.Text: Thanks for the pointer. I'll take a look at that. However, if I go ahead and add a value to the accolades field in the control panel (and save the entry), then this loading code works.
Comments.UserDisplayName: mooreds
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-24T03:32:22.513Z
Comments.Text: Because you are in the correct entry... That's what I'm trying to say the entire time
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-26T22:29:47.257Z
Comments.Text: But I'm guaranteed the way I look up the entry to have it be valid (because I'm looking on a slug and I know that the slug is unique in the system)
Comments.UserDisplayName: mooreds
Tags: <craft3><field><migration>
Title: Unable to add data via migration to a new field
ThreadText: # Unable to add data via migration to a new field
  
  (Asked by: username_0 on 2018-10-17)
  
  When I am creating a new field (and I've tried plain text, redactor, supertable and matrix field) and associating it with an existing entry type (using the Control Panel to do so), I run into an issue where I can't load data to the field with a migration.
  
  Here's code from the migration I might try to run to put some data in this field (in this case, it is a redactor).
  
  ```
    foreach ($attorneys as $attorney) { // iterate over json data we are going to import.
      $craft_attorney_id = // lookup attorney id
      $entry = Entry::find()->id($craft_attorney_id)->status(['enabled','disabled'])->one();
      if (!$entry) {
        continue;
      }
      $accolades = 'test text';
      $entry->setFieldValues(array('accolades' => $accolades));
      if(!\Craft::$app->elements->saveElement($entry)) {
          throw new \Exception("Couldn't save entry: " . print_r($entry->getErrors(), true));
      }
    }
  
  ```
  
  When I view one entry in the CP and enter data in the new field, and then rerun the migration (take it down then up), the new data from the migration is saved just fine (for that element. but not for any others).
  
  I've set the redactor field to 'full html', and also stripped the tags from the field to be inserted.
  
  Our content table has 56 columns, but many are text, so I don't think I'm running into an issue with row length ( <https://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html#innodb-maximums-minimums> )
  
  Tried clearing all the caches.
  
  Tried upgrading from 3.0.9 to 3.0.27.
  
  Also tried to refresh fields on the fields service: `\Craft::$app->fields->refreshFields();` at the beginning of the migraiton.
  
  If I try to read from the entry in the same migration, I get this exception
  
  ```
  Invalid field handle: accolades (/.../vendor/craftcms/cms/src/base/Element.php:1932)
  #0 /../vendor/craftcms/cms/src/base/Element.php(1612): craft\base\Element->normalizeFieldValue('accolades')
  #1 /../migrations/m181016_224151_import_accolades_v2.php(59): craft\base\Element->getFieldValue('accolades')
  #2 /../vendor/craftcms/cms/src/db/Migration.php(56): craft\contentmigrations\m181016_224151_import_accolades_v2->safeUp()
  
  ```
  
  Except I can find that field using the fieldService. Maybe it is something to do with the normalization?
  
  Am I saving the entry incorrectly? Do I need to do some kind of special call to refresh the element mapping of fields?
  
  Thanks!
  
  **Tags:** craft3, field, migration
  
  > **Comments:**
  > **username_1**: Could you please explain the first two lines of your code? I'm not sure if you have a creative way to fetch your entries of if you don't know what's happening exactly  
  > **username_0**: Hi @username_1, sorry, I should have explained that. I'm just pulling from a json file (previously parsed) and looking up the existing entry id by the name that is pulled from the json file. I will update the code sample.  
  > **username_1**: The "problem" I see is: if it doesn't find a valid id the value might be `null` or `0` and if that's the case Craft will ignore the parameter and instead of returning `null` as well it will return the first found Entry that might not have the field in it's layout. Can you really make sure every title is 100% bound to an entry id?  
  > **username_0**: If we don't see an entry, we `continue`. At least that is the intention, so that we always have a valid entry to associate the data with. I thought the `if (!$entry) {` line did that. Or am I missing your point?  
  > **username_1**: My point is you should check if your id is valid as well. If id is null Craft will return the first entry it can find that might have a different layout because it can be in a different section  
  > **username_0**: Thanks for the pointer. I'll take a look at that. However, if I go ahead and add a value to the accolades field in the control panel (and save the entry), then this loading code works.  
  > **username_1**: Because you are in the correct entry... That's what I'm trying to say the entire time  
  > **username_0**: But I'm guaranteed the way I look up the entry to have it be valid (because I'm looking on a slug and I know that the slug is unique in the system)  
  
  ---
  
  ## 0 answers
  
  