<p>Just a hunch and not tested, but don't do this:</p>

<pre><code>$group-&gt;id   = '';
</code></pre>

<p>My guess is by explicitly setting that to an empty string, Craft thinks it's dealing with an existing record and not a new one.</p>

CC BY-SA 3.0
2015-10-22T19:27:05.893Z
Initially I didn't have that there and added it in attempt to resolve the problem. I get the same error both ways.
jpwdesigns
CC BY-SA 3.0
2015-10-22T04:08:15.140Z
2015-10-22T04:08:15.140Z
Brad Bell
<p>I'm trying to create a field group in a plugin that fires onAfterInstall. I'm getting the error below but after install the field group does exist and seems to have been created successfully. </p>

<pre><code>2015/10/21 21:57:17 [warning] [application] [Forced] Failed installing XXXXXX: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft`.`craft_fieldlayoutfields`, CONSTRAINT `craft_fieldlayoutfields_fieldId_fk` FOREIGN KEY (`fieldId`) REFERENCES `craft_fields` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_fieldlayoutfields` (`required`, `uid`, `layoutId`, `tabId`, `sortOrder`, `dateUpdated`, `dateCreated`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6). Bound with :yp0=0, :yp1='b68bdc0b-647d-45fd-afa1-dbadffb973b8', :yp2=427, :yp3=263, :yp4=2, :yp5='2015-10-21 21:57:17', :yp6='2015-10-21 21:57:17'
</code></pre>

<p>Here is what the code look like which is creating the field group:</p>

<pre><code>// XXXXX field group
Craft::log('Creating the XXXXX field group.',
 LogLevel::Info, true, 'application', 'XXXXX');

$group = new FieldGroupModel();
$group-&gt;id   = '';
$group-&gt;name = 'XXXXXXX';

if (craft()-&gt;fields-&gt;saveGroup($group))
{
  Craft::log('XXXXXX field group created successfully.',
   LogLevel::Info, true, 'application', 'XXXXXX');
}
else
{
  Craft::log('Could not save the XXXXXX field group.',
   LogLevel::Warning, true, 'application', 'XXXXXX');
}
</code></pre>

CC BY-SA 3.0
2015-10-22T20:39:09.060Z
Not sure what the issue was but I'm not seeing this problem today. It may be that this field group was not uninstalled correctly prior causing an error with saving the same group name twice? Anyway it's a non-issue.
jpwdesigns
<plugin-development><plugins>
Create a field group in a plugin
# Create a field group in a plugin

(Asked by: username_0 on 2015-10-21)

I'm trying to create a field group in a plugin that fires onAfterInstall. I'm getting the error below but after install the field group does exist and seems to have been created successfully. 
2015/10/21 21:57:17 [warning] [application] [Forced] Failed installing XXXXXX: An exception was thrown: CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`craft`.`craft_fieldlayoutfields`, CONSTRAINT `craft_fieldlayoutfields_fieldId_fk` FOREIGN KEY (`fieldId`) REFERENCES `craft_fields` (`id`) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO `craft_fieldlayoutfields` (`required`, `uid`, `layoutId`, `tabId`, `sortOrder`, `dateUpdated`, `dateCreated`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6). Bound with :yp0=0, :yp1='b68bdc0b-647d-45fd-afa1-dbadffb973b8', :yp2=427, :yp3=263, :yp4=2, :yp5='2015-10-21 21:57:17', :yp6='2015-10-21 21:57:17'

Here is what the code look like which is creating the field group:
// XXXXX field group
Craft::log('Creating the XXXXX field group.',
 LogLevel::Info, true, 'application', 'XXXXX');

$group = new FieldGroupModel();
$group->id   = '';
$group->name = 'XXXXXXX';

if (craft()->fields->saveGroup($group))
{
  Craft::log('XXXXXX field group created successfully.',
   LogLevel::Info, true, 'application', 'XXXXXX');
}
else
{
  Craft::log('Could not save the XXXXXX field group.',
   LogLevel::Warning, true, 'application', 'XXXXXX');
}

**Tags:** plugin-development, plugins

> **Comments:**
> **username_0**: Not sure what the issue was but I'm not seeing this problem today. It may be that this field group was not uninstalled correctly prior causing an error with saving the same group name twice? Anyway it's a non-issue.  

---

## 1 answers

### Answer by username_1 on 2015-10-22. Score: 1

Just a hunch and not tested, but don't do this:
$group->id   = '';

My guess is by explicitly setting that to an empty string, Craft thinks it's dealing with an existing record and not a new one.

> **Comments:**
> **username_0**: Initially I didn't have that there and added it in attempt to resolve the problem. I get the same error both ways.  

---

