1

I want to create a custom table using db_schema.xml like that contains column like this:

enter image description here enter image description here

I tried to do it like this:

<column name="description" nullable="false" default="null" xsi:type="text"/>

but always got an error like this:

The XML in file "/var/www/M2/app/code/Vendor/Module/etc/db_schema.xml" is invalid:
Element 'column', attribute 'default': The attribute 'default' is not allowed.
4
  • What error you are getting?
    – aravind
    Feb 17, 2020 at 8:26
  • @aravind see my updated question
    – jojo
    Feb 18, 2020 at 3:07
  • change nullable="false" to nullable="true"
    – Sohel Rana
    Feb 18, 2020 at 3:28
  • @SohelRana i need this column not nullable with default value None
    – jojo
    Feb 18, 2020 at 3:52

6 Answers 6

3

For Magento2.3 :

Add below code in file db_schema.xml.

<column xsi:type="text" name="description" nullable="true" comment="Description"/>

Then run below command.

php bin/magento setup:db-declaration:generate-whitelist --module-name=VendorName_ModuleName

This will add new column "description" in table with default value "NULL". No need to specify default="NULL" in schema definition.

1
  • Right on! I just ran into this same problem. Had a bunch of column values with the string "NULL" in them. Thanks for this.
    – sdaugherty
    Mar 17, 2021 at 20:36
2

I think the problem is nullable="false" your setting the column should not be null and you are setting its default value to null, so, here you are creating a conflict for an interpreter I guess. Try the below code and let us know the result.

<column xsi:type="text" name="description" nullable="true" default="NULL" comment="Description"/>
1

run this command

php bin/magento setup:db-declaration:generate-whitelist --module-name=VendorName_ModuleName

then change this

<column xsi:type="text" name="description" nullable="false" comment="Description"/>

and finally run this command

php bin/magento setup:upgrade
1

If we need to pass default value, then we need to check whether the default value is compatible with the type of field we are using is

default Initializes the column with the specified default value. The default value should have the same datatype defined in xsi:type.

so for having any int value as default use it like

<column xsi:type="int" name="item_seq" nullable="true" default="99" comment="Item Sequence"/>

For more info, you can consider following link https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/db-schema.html

0

I know I'm a bit late to the party, but I just ran into this issue today while using Magento 2.4.1. I'm pretty sure the answer is you can't set a default value for a column with type of text/mediumtext/longtext. None of those schema files (found under vendor\magento\framework\Setup\Declaration\Schema\etc\types\texts) include a "default" attribute definition. You can probably update the schema by including your own .xsd file to define a "default" attribute, but short of that this isn't possible. I would note however that while the website is in "default" mode you can run your "setup:upgrade" command just fine with the "default" attribute in your db_schema.xml file. Though the fact that in "developer" mode these validations fail seem to indicate trying to circumspect the issue this way is not a good idea. (I am pretty new to Magento 2 so take this answer with a grain of salt.)

-1

Change: xsi:type="text" <==to==> xsi:type="varchar"

Ex:

<column xsi:type="varchar" name="image_position" nullable="true" default="full" comment="image_position"/> 

Then run

php bin/magento setup:db-declaration:generate-whitelist --module-name=MyCompany_MyModule

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.