0

https://developer.adobe.com/commerce/php/development/components/declarative-schema/configuration/#column-subnode

I didn't find the char type (only varchar) in the column section but in the length section char is present.

I tried to use char myself like so:

<column xsi:type="char" name="hash" nullable="false" length="32" comment="MD5"/>

and it seems to properly create a column in the database char(32) in the table

QUESTION: Is it supported/recommended in Magento2 to use the char type in db_schema.xml and documentation is just not full enough, or it is indeed not supported so there is no mistake in the documentation?

Thank you.

2 Answers 2

1

Document is correct

You can get actual data from below file. Please check it.

vendor/magento/framework/Setup/Declaration/Schema/etc/types/texts/char.xsd

Here plain text can be persisted without trailing spaces. The length of this field can't be more than 255 characters. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.

db_schema.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="test_char" resource="default" engine="innodb" comment="TEST CHAR DATA TYPE">
        <column xsi:type="smallint" name="id" padding="6" unsigned="false" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="char" name="hash_val" nullable="false" length="255" comment="MD5"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
    </table>
</schema>

Example Table Structure:

enter image description here

CHAR VS VARCHAR

0

Magento 2 recommended below format.

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="my_table" resource="default" engine="innodb" comment="My Table">
        <column xsi:type="int" name="my_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity Id"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="my_id"/>
        </constraint>
        <column name="title" nullable="true" xsi:type="varchar" comment="Title" length="255"/>
        <column name="description" nullable="true" xsi:type="text" comment="Description"/>
    </table>
</schema>

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.