9

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.

0

4 Answers 4

8

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.

1
  • 1
    This. I just poked around some old 1.5 beta code bases and the ATTRIBUTE_CODE_MAX_LENGTH constant didn't exist. Commented Sep 4, 2013 at 5:11
7

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.

1
  • As perfect an example as this might have been, I think @xyphoid has the less cranky (and correct) answer. Commented Sep 5, 2013 at 3:46
0

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 .

-5

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.

5
  • 4
    A core modification?! Surely you jest.
    – philwinkle
    Commented Sep 3, 2013 at 23:32
  • 2
    Why 60 and not 212?
    – Marius
    Commented Sep 4, 2013 at 6:12
  • 2
    Why not 5000000? Commented Apr 21, 2014 at 21:05
  • You will not be able to save attribut options when editing products if you do so...
    – loeffel
    Commented Feb 17, 2017 at 12:26
  • @Marius : EAV attribute code will be transformed into column name for flat tables and MySQL does not support column name with more than 64 symbols
    – Nolwennig
    Commented Jul 17, 2017 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.