Question

I have a magento site with many custom options. When the product is created, sometimes the sku is longer than 255 characters.

The sku I'm refering to is in "sales_flat_order_item" and a few other places I believe.

Is it safe to change these fields to "text" instead of "varchar255"?

Was it helpful?

Solution

If you run the following query against the database you can find all of the tables which have an sku column:

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('sku') AND TABLE_SCHEMA = 'your_database';

You will find the following returned:

catalog_product_entity - varchar(64)
catalog_product_flat_1 - varchar(64)
catalog_product_flat_2 - varchar(64)
catalog_product_flat_x // as many flat tables as there are stores
catalog_product_option - varchar(64)
catalog_product_option_type_value - varchar(64)
sales_flat_creditmemo_item - varchar(255)
sales_flat_invoice_item - varchar(255)
sales_flat_order_item - varchar(255)
sales_flat_quote_address_item - varchar(255)
sales_flat_quote_item - varchar(255)
sales_flat_shipment_item - varchar(255)

So a fair few tables. Although making the sku field longer is not something I have tried, I don't see any particular reason why it won't work, but you should approach the task carefully with plenty of testing.

No idea why but it seems sku is stored as a 64 character long field in relation to products, and a 255 character long field in relation to ordered items - so you are essentially restricted to 64 characters by default.

My advice would be to write an install script to change these columns, something along the lines of the following:

<?php
$installer = $this;
$installer->startSetup();

$installer->getConnection()->changeColumn(
    $installer->getTable('module/table'),
    'sku',
    'sku',
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
        'length' => '64k',
        'comment' => 'SKU'
    )
);

// more changeColumn() calls for each table

$installer->endSetup();

A few gotcha's I would look out for:

  • There may be some PHP based restrictions on the length of content saved to these fields, have to test to be sure
  • Watch out if you upgrade the store, some of these columns could be reset to 64/255 characters and data will irrecoverably be lost in the process if this is the case
  • Be careful when adding new stores as a new flat product table will be created so you will need to check the field is of the correct length.

OTHER TIPS

Yes it is. Magento doesn't do any checks on this afaik. So if it is longer than 255, mysql doesn'T raise an error and just cuts this (f*cking mysql)

so, just write an install script to change this and you won'T have problems in the future, as long as the sku is shorter than TEXT length (16 mio?)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top