Question

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.

No correct solution

OTHER TIPS

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"/>

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.

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

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

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.)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top