Create a field group in a plugin 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-&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');
}
Just a hunch and not tested, but don't do this:

$group-&gt;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.