For the most part, Magento internals handle initial table creation with raw SQL on setup:
app/code/core/Enterprise/Cms/sql/enterprise_cms_setup/mysql4-install-1.6.0.0.0.php
$installer->run("CREATE TABLE IF NOT EXISTS `{$installer->getTable('enterprise_cms/hierarchy_metadata')}` (
`node_id` INT(10) UNSIGNED NOT NULL,
`meta_first_last` TINYINT(1) NOT NULL DEFAULT '0',
`meta_next_previous` TINYINT(1) NOT NULL DEFAULT '0',
`meta_chapter` TINYINT(1) NOT NULL DEFAULT '0',
`meta_section` TINYINT(1) NOT NULL DEFAULT '0',
`pager_visibility` TINYINT(4) UNSIGNED NOT NULL,
`pager_frame` SMALLINT(6) UNSIGNED NOT NULL,
`pager_jump` SMALLINT(6) UNSIGNED NOT NULL,
`menu_visibility` TINYINT(4) UNSIGNED NOT NULL,
`menu_levels_up` TINYINT(4) UNSIGNED NOT NULL,
`menu_levels_down` TINYINT(4) UNSIGNED NOT NULL,
`menu_ordered` TINYINT(4) UNSIGNED NOT NULL,
`menu_list_type` VARCHAR(50) NOT NULL DEFAULT '',
PRIMARY KEY (`node_id`),
CONSTRAINT `FK_ENTERPRISE_CMS_HIERARCHY_METADATA_NODE` FOREIGN KEY (`node_id`) REFERENCES `{$installer->getTable('enterprise_cms/hierarchy_node')}` (`node_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
You see the TINYINT type being added quite often via addColumn
which is available through getConnection()
in $installer
:
Mage/Core/Catalog/sql/catalog_setup/mysql4-upgrade-0.7.72-0.7.73.php
$installer->getConnection()->addColumn($installer->getTable('catalog/product_super_attribute_label'),
'use_default', 'tinyint(1) UNSIGNED DEFAULT 0 AFTER store_id');
Which is usually called on an existing table.
There's nothing wrong with using TINYINT(1) as a boolean. If you're dead set against the SHOW CREATE copy-pasta method of popuplating table schema with $installer->run
, avoid the setup woes you're having and run the addColumn later as an update in your module:
mysql4-upgrade-0.1.0-0.1.1:
$installer->getConnection()->addColumn($installer->getTable('yourresource/model'),
'is_active', 'TINYINT(1) UNSIGNED DEFAULT 0 AFTER store_id');
The problem here is part of the resource save function magento checks if the primary key is set to auto increment and then removes it from the data being saved if this is the case.
In Mage_Core_Model_Resource_Db_Abstract::save
you can see how it deals with $this->_isPkAutoIncrement
/**
* Not auto increment primary key support
*/
if ($this->_isPkAutoIncrement) {
$data = $this->_prepareDataForSave($object);
unset($data[$this->getIdFieldName()]);
$this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
} else {
$select = $this->_getWriteAdapter()->select()
->from($this->getMainTable(), array($this->getIdFieldName()))
->where($condition);
if ($this->_getWriteAdapter()->fetchOne($select) !== false) {
$data = $this->_prepareDataForSave($object);
unset($data[$this->getIdFieldName()]);
if (!empty($data)) {
$this->_getWriteAdapter()->update($this->getMainTable(), $data, $condition);
}
} else {
$this->_getWriteAdapter()->insert($this->getMainTable(), $this->_prepareDataForSave($object));
}
}
So to fix my issue I simply have to set $_isPkAutoIncrement
on my Model's resource to false and Magento will keep the PK in the data and save it into the table.
Best Answer
You need to write this code in your Resource model class
for example