Answers.Body: <p>The easiest solution for this by far is to just run the <code>$model-&gt;getErrors()</code> function in order to see what's wrong.. Then you'll see the following: </p>
  
  <blockquote>
    <p>"Type is invalid."</p>
  </blockquote>
  
  <p>Take a look at your <code>Section</code> model</p>
  
  <pre><code>const TYPE_CHANNEL = 'channel';
  </code></pre>
  
  <p>change </p>
  
  <pre><code>'type' =&gt; 'Channel',
  </code></pre>
  
  <p>to</p>
  
  <pre><code>'type' =&gt; 'channel',
  </code></pre>
  
  <p>or even better</p>
  
  <pre><code>'type' =&gt; Section::TYPE_CHANNEL,
  </code></pre>
  
  <p>Also: every <code>save</code> function returns a variable of type <code>boolean</code> if it was successful or not. You should <strong>always</strong> check for those values.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-29T06:59:20.973Z
Answers.Text: Thanks for pointing me in the right direction. The incorrect case for channel was an artefact of trying a hundred different ways to get this to work, however, it was this and another error causing the issue. The tip about `$model->getErrors()` is really useful.
  
  Using
  
  `'type' => Section::TYPE_CHANNEL,` 
  
  work a treat.
  
  Thanks
Answers.UserDisplayName: Simon Leadbetter
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-28T21:02:48.720Z
Answers.LastActivityDate: 2018-05-01T04:55:17.870Z
Answers.LastEditDate: 2018-05-01T04:55:17.870Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>As documentation for Migrations is pretty slim, here is the working code:</p>
  
  <pre><code>&lt;?php
  
  namespace craft\contentmigrations;
  
  use Craft;
  use craft\db\Migration;
  
  /**
   * m180428_195138_test migration.
   */
  class m180428_195138_test extends Migration
  {
      /**
       * @inheritdoc
       */
      public function safeUp()
      {
  
  
        if (is_null(Craft::$app-&gt;volumes-&gt;getVolumeByHandle("newvolume"))) {
          $volume = new \craft\volumes\Local([
            "name" =&gt; "New volume",
            "handle" =&gt; "newvolume",
            "hasUrls" =&gt; true,
            "url" =&gt; "@web/images/newvolume",
            "path" =&gt; "@webroot/images/newvolume"
          ]);
  
          $success = Craft::$app-&gt;volumes-&gt;saveVolume($volume);
  
        }
  
        if (is_null(Craft::$app-&gt;sections-&gt;getSectionByHandle("awesomeNews"))) {
          $section = new \craft\models\Section([
            "name" =&gt; "Awesome News",
            "handle" =&gt; "awesomeNews",
            "type" =&gt; \craft\models\Section::TYPE_CHANNEL,
            "siteSettings" =&gt; [
                new \craft\models\Section_SiteSettings([
                    "siteId" =&gt; Craft::$app-&gt;sites-&gt;getPrimarySite()-&gt;id,
                    "enabledByDefault" =&gt; true,
                    "hasUrls" =&gt; true,
                    "uriFormat" =&gt; "foo/{slug}",
                    "template" =&gt; "foo/_entry",
                ]),
            ]
          ]);
  
          $success = Craft::$app-&gt;sections-&gt;saveSection($section);
  
          $errors = $section-&gt;getErrors();
          foreach ($errors as $error) {
            echo $error[0];
          }
  
        }
  
        return $success;
  
      }
  
      /**
       * @inheritdoc
       */
      public function safeDown()
      {
  
        $newvolume = Craft::$app-&gt;volumes-&gt;getVolumeByHandle("newvolume");
        if (!is_null($newvolume)) {
          $success = Craft::$app-&gt;volumes-&gt;deleteVolumeById($newvolume-&gt;id);
        }
  
        $awesomeNews = Craft::$app-&gt;sections-&gt;getSectionByHandle("awesomeNews");
        if (!is_null($awesomeNews)) {
          $success = Craft::$app-&gt;sections-&gt;deleteSectionById($awesomeNews-&gt;id);
        }
  
        return $success;
  
      }
  
  
  }
  </code></pre>
  
  <p>This includes the error checking when creating the new section and the <code>save</code> functions are assigned to a variable which are returned at the end.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-29T07:42:16.740Z
Answers.Text: Just a side note: migrations are one of the few things that are really well documented https://www.yiiframework.com/doc/guide/2.0/en/db-migrations since Craft doesn't handle them
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-29T07:04:52.067Z
Answers.LastActivityDate: 2018-04-29T07:04:52.067Z
Answers.UserDisplayName: Simon Leadbetter
Body: <p>I have been trying to get my head wrapped around Migrations in Craft 3 and find myself struggling with added a new section.</p>
  
  <p>I have managed to add a new volume, but not a section. Here’s where I have got to so far…</p>
  
  <pre><code>&lt;?php
  
  namespace craft\contentmigrations;
  
  use Craft;
  use craft\db\Migration;
  
  /**
   * m180428_195138_test migration.
   */
  class m180428_195138_test extends Migration
  {
      /**
       * @inheritdoc
       */
      public function safeUp()
      {
  
        if (is_null(Craft::$app-&gt;volumes-&gt;getVolumeByHandle("newvolume"))) {
          $volume = new \craft\volumes\Local([
            "name" =&gt; "New volume",
            "handle" =&gt; "newvolume",
            "hasUrls" =&gt; true,
            "url" =&gt; "@web/images/newvolume",
            "path" =&gt; "@webroot/images/newvolume"
          ]);
  
          Craft::$app-&gt;volumes-&gt;saveVolume($volume);
        }
  
  
        if (is_null(Craft::$app-&gt;sections-&gt;getSectionByHandle("awesomeNews"))) {
          $section = new \craft\models\Section([
            'name' =&gt; 'Awesome News',
            'handle' =&gt; 'awesomeNews',
            'type' =&gt; 'Channel',
            'siteSettings' =&gt; [
                new \craft\models\Section_SiteSettings([
                    'siteId' =&gt; Craft::$app-&gt;sites-&gt;getPrimarySite()-&gt;id,
                    'enabledByDefault' =&gt; true,
                    'hasUrls' =&gt; true,
                    'uriFormat' =&gt; 'foo/{slug}',
                    'template' =&gt; 'foo/_entry',
                ]),
            ]
          ], true);
  
          Craft::$app-&gt;sections-&gt;saveSection($section);
  
        }
  
  
      }
  
      /**
       * @inheritdoc
       */
      public function safeDown()
      {
  
        $newvolume = Craft::$app-&gt;volumes-&gt;getVolumeByHandle("newvolume");
        if (!is_null($newvolume)) {
          Craft::$app-&gt;volumes-&gt;deleteVolumeById($newvolume-&gt;id);
        }
  
        $awesomeNews = Craft::$app-&gt;sections-&gt;getSectionByHandle("awesomeNews");
        if (!is_null($awesomeNews)) {
          Craft::$app-&gt;volumes-&gt;deleteSectionById($awesomeNews-&gt;id);
        }
  
      }
  }
  </code></pre>
  
  <p>The migration states it has been successful, however, I only see the new volume, not the section. I have tried to execute this with only the block relating to the section.</p>
  
  <p>Does anyone know if it is possible to create a new section with Migrations? If it is, any idea what may be wrong with the above?</p>
  
Tags: <craft3><migration>
Title: Add new section in Craft 3 Migrations
ThreadText: # Add new section in Craft 3 Migrations
  
  (Asked by: username_0 on 2018-04-28)
  
  I have been trying to get my head wrapped around Migrations in Craft 3 and find myself struggling with added a new section.
  
  I have managed to add a new volume, but not a section. Here’s where I have got to so far…
  
  ```
  <?php
  
  namespace craft\contentmigrations;
  
  use Craft;
  use craft\db\Migration;
  
  /**
   * m180428_195138_test migration.
   */
  class m180428_195138_test extends Migration
  {
      /**
       * @inheritdoc
       */
      public function safeUp()
      {
  
        if (is_null(Craft::$app->volumes->getVolumeByHandle("newvolume"))) {
          $volume = new \craft\volumes\Local([
            "name" => "New volume",
            "handle" => "newvolume",
            "hasUrls" => true,
            "url" => "@web/images/newvolume",
            "path" => "@webroot/images/newvolume"
          ]);
  
          Craft::$app->volumes->saveVolume($volume);
        }
  
        if (is_null(Craft::$app->sections->getSectionByHandle("awesomeNews"))) {
          $section = new \craft\models\Section([
            'name' => 'Awesome News',
            'handle' => 'awesomeNews',
            'type' => 'Channel',
            'siteSettings' => [
                new \craft\models\Section_SiteSettings([
                    'siteId' => Craft::$app->sites->getPrimarySite()->id,
                    'enabledByDefault' => true,
                    'hasUrls' => true,
                    'uriFormat' => 'foo/{slug}',
                    'template' => 'foo/_entry',
                ]),
            ]
          ], true);
  
          Craft::$app->sections->saveSection($section);
  
        }
  
      }
  
      /**
       * @inheritdoc
       */
      public function safeDown()
      {
  
        $newvolume = Craft::$app->volumes->getVolumeByHandle("newvolume");
        if (!is_null($newvolume)) {
          Craft::$app->volumes->deleteVolumeById($newvolume->id);
        }
  
        $awesomeNews = Craft::$app->sections->getSectionByHandle("awesomeNews");
        if (!is_null($awesomeNews)) {
          Craft::$app->volumes->deleteSectionById($awesomeNews->id);
        }
  
      }
  }
  
  ```
  
  The migration states it has been successful, however, I only see the new volume, not the section. I have tried to execute this with only the block relating to the section.
  
  Does anyone know if it is possible to create a new section with Migrations? If it is, any idea what may be wrong with the above?
  
  **Tags:** craft3, migration
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-04-28. Score: 1 | Accepted answer
  
  The easiest solution for this by far is to just run the `$model->getErrors()` function in order to see what's wrong.. Then you'll see the following:
  
  > "Type is invalid."
  
  Take a look at your `Section` model
  
  ```
  const TYPE_CHANNEL = 'channel';
  
  ```
  
  change
  
  ```
  'type' => 'Channel',
  
  ```
  
  to
  
  ```
  'type' => 'channel',
  
  ```
  
  or even better
  
  ```
  'type' => Section::TYPE_CHANNEL,
  
  ```
  
  Also: every `save` function returns a variable of type `boolean` if it was successful or not. You should **always** check for those values.
  
  > **Comments:**
  > **username_0**: Thanks for pointing me in the right direction. The incorrect case for channel was an artefact of trying a hundred different ways to get this to work, however, it was this and another error causing the issue. The tip about `$model->getErrors()` is really useful.  Using  `'type' => Section::TYPE_CHANNEL,`   work a treat.  Thanks  
  
  ---
  
  ### Answer by username_0 on 2018-04-29. Score: 2
  
  As documentation for Migrations is pretty slim, here is the working code:
  
  ```
  <?php
  
  namespace craft\contentmigrations;
  
  use Craft;
  use craft\db\Migration;
  
  /**
   * m180428_195138_test migration.
   */
  class m180428_195138_test extends Migration
  {
      /**
       * @inheritdoc
       */
      public function safeUp()
      {
  
        if (is_null(Craft::$app->volumes->getVolumeByHandle("newvolume"))) {
          $volume = new \craft\volumes\Local([
            "name" => "New volume",
            "handle" => "newvolume",
            "hasUrls" => true,
            "url" => "@web/images/newvolume",
            "path" => "@webroot/images/newvolume"
          ]);
  
          $success = Craft::$app->volumes->saveVolume($volume);
  
        }
  
        if (is_null(Craft::$app->sections->getSectionByHandle("awesomeNews"))) {
          $section = new \craft\models\Section([
            "name" => "Awesome News",
            "handle" => "awesomeNews",
            "type" => \craft\models\Section::TYPE_CHANNEL,
            "siteSettings" => [
                new \craft\models\Section_SiteSettings([
                    "siteId" => Craft::$app->sites->getPrimarySite()->id,
                    "enabledByDefault" => true,
                    "hasUrls" => true,
                    "uriFormat" => "foo/{slug}",
                    "template" => "foo/_entry",
                ]),
            ]
          ]);
  
          $success = Craft::$app->sections->saveSection($section);
  
          $errors = $section->getErrors();
          foreach ($errors as $error) {
            echo $error[0];
          }
  
        }
  
        return $success;
  
      }
  
      /**
       * @inheritdoc
       */
      public function safeDown()
      {
  
        $newvolume = Craft::$app->volumes->getVolumeByHandle("newvolume");
        if (!is_null($newvolume)) {
          $success = Craft::$app->volumes->deleteVolumeById($newvolume->id);
        }
  
        $awesomeNews = Craft::$app->sections->getSectionByHandle("awesomeNews");
        if (!is_null($awesomeNews)) {
          $success = Craft::$app->sections->deleteSectionById($awesomeNews->id);
        }
  
        return $success;
  
      }
  
  }
  
  ```
  
  This includes the error checking when creating the new section and the `save` functions are assigned to a variable which are returned at the end.
  
  > **Comments:**
  > **username_1**: Just a side note: migrations are one of the few things that are really well documented https://www.yiiframework.com/doc/guide/2.0/en/db-migrations since Craft doesn't handle them  
  
  ---
  
  