Question

When trying to create attributes, I've discovered that the maximum length of an attribute code is 30 characters. Turns out though, that this is NOT actually some sort of real restriction - it is a completely arbitrary value, defined by

Mage_Eav_Model_Entity_Attribute::ATTRIBUTE_CODE_MAX_LENGTH.

So given that, why is there a restriction at all? And why is the default 30, rather than for instance 255, which is the actual limit of the database column?

PS. If anyone can think of better tags for this question, please update them.

Was it helpful?

Solution

This was almost certainly changed in 1.6 part of adding Oracle support - in Oracle, columns can only be 30 characters long, so lots of Magento attributes got shortened, and I imagine this restriction was added at the same time.

See http://m-chanaan.hr/wp-content/uploads/2013/04/RDBMS_Guide2.pdf for a lot of discussion on this.

OTHER TIPS

A perfect example of teams or individual developers not talking to one another. While the main eav_attribute table's atrtibute_code is a varchar(255), this code value is often used in other tables.

In catalog_product_link_attribute there's a product_link_attribute_code attribute (which is the attribute code), and this column is a varchar(32). Back in prehistoric times when the sales objects were EAV objects, they had an attribute_code column which had varchar(50) as a length.

# Mage/Sales/sql/sales_setup/mysql4-upgrade-0.9.45-0.9.46.php
$installer->getConnection()->addColumn($this->getTable('sales_order'), $attribute['attribute_code'], 'varchar(50) NULL');

I imagine there's others as well.

Without an actual specification or agreement on what was being built, the developer in charge of the UI for the attribute section likely looked at all the attribute_code columns, picked the shortest one, and enforced a length to make sure users couldn't create an attribute code that would be too long for one of the various tables other developers were working on.

As for why a developer would choose a varchar length that wasn't 255 — there's a school of thought about database design that says you only make your columns as long as they need to be to save disk space, reduce RAM, be more efficient in join operations, etc. Some developers still hold to this vs. the modern trend of "make it as large as possible and worry about performance implications later". It's clear there was disagreement on the maximum length of a varchar for attribute_code's among the Magento core team at one point, and now it lives on in legacy code.

Like xyphoid says, the previous limitation was caused when Oracle BD was supported because in Oracle, columns can only be 30 characters long.

Now,

After okorshenko core modification (PR#10225)

const ATTRIBUTE_CODE_MAX_LENGTH = 60;

The following table describes the maximum length for each type of identifier.

| Identifier | Maximum Length (characters) |
|------------|-----------------------------|
| Column     | 64                          |

The value is defined as 60 because in the flat mode attribute code will be transformed into column name. MySQL allows only 64 symbols in column name .

To resolve this error, please use this code

CONST ATTRIBUTE_CODE_MAX_LENGTH = 30; to 60

The code should be

CONST ATTRIBUTE_CODE_MAX_LENGTH = 60;

This will solve your issue.

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