Assign a category group to a category field via migration I am writing a plugin that sets up Craft in a particular way and have created a category field, but can't figure out how to assign a category group to the field, I have the following:

 protected function createCategoriesField() {

        // Get the field group
        $group = (new \craft\db\Query())
            -&gt;select("id")
            -&gt;from("fieldgroups")
            -&gt;where(["name" =&gt; "common"])
            -&gt;one();

        // Get the category group
        $catGroup = Craft::$app-&gt;categories-&gt;getGroupByHandle("categories");


        // Initialize the field
        $field = new \craft\fields\Categories([
            "groupId" =&gt; $group["id"],
            "name" =&gt; "Categories",
            "handle" =&gt; "categories",
            "selectionLabel" =&gt; "Select categories",
            "sources" =&gt; $catGroup
        ]);

        // Save the field
        return (Craft::$app-&gt;getFields()-&gt;saveField($field));
    }


It seems sources is the correct setting, but it accepts 'source keys' which I'm unfamiliar with
Figured it out, the format it expects is 'source' =&gt; 'group: &lt;group UID&gt;'

So...

    protected function createCategoriesField() {
        // Get the group
        $group = (new \craft\db\Query())
            -&gt;select("id")
            -&gt;from("fieldgroups")
            -&gt;where(["name" =&gt; "common"])
            -&gt;one();

        // Get the category group UID
        $catGroup = Craft::$app-&gt;categories-&gt;getGroupByHandle("categories")-&gt;uid;

        // Initialize the field
        $field = new \craft\fields\Categories([
            "groupId" =&gt; $group["id"],
            "name" =&gt; "Categories",
            "handle" =&gt; "categories",
            "selectionLabel" =&gt; "Select categories",
            "source" =&gt; "group:" . $catGroup
        ]);

        // Save the field
        return (Craft::$app-&gt;getFields()-&gt;saveField($field));
    }


This was, and is, going to be a big help with migrations -> https://github.com/markhuot/craftql/blob/master/src/Console/ToolsController.php