Declaring Custom Database Tables in Magento 2 with db_schema.xml
Use Magento 2 declarative schema to add custom tables and columns without install scripts, plus how to whitelist removals and verify setup:upgrade.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Use Magento 2 declarative schema to add custom tables and columns without install scripts, plus how to whitelist removals and verify setup:upgrade.
A practical architecture note for Magento custom modules: use etc/db_schema.xml for DDL, idempotent data patches for DML, and setup:upgrade as the single deployment path.
Declarative schema in Magento 2 replaces fragile upgrade scripts with a single source of truth in db_schema.xml. Here's how to add a column, backfill it with a Data Patch, and verify the result.
Replace legacy Install/Upgrade scripts with db_schema.xml: the minimal design, the whitelist trust boundary, operational checks, and the failure modes that bite in production.
Magento 2 Declarative Schema replaces procedural upgrade scripts with idempotent XML declarations. This guide covers the mechanism, a worked example for a custom order table, whitelist protection, data patches, and practical limits like type changes and large-table locks.
Learn how Magento’s declarative schema lets you define database changes in XML, letting setup:upgrade generate the needed ALTER scripts automatically.
Stop writing procedural PHP upgrade scripts. Learn how Magento 2 Declarative Schema uses db_schema.xml to manage database states and simplify team collaboration.
Step‑by‑step guide to add a database column using db_schema.xml, run setup:upgrade, verify the change, and recover if needed.
Magento's declarative schema in db_schema.xml defines a desired database state, but setup:upgrade does not provide a built-in command to undo an applied schema change. The platform intentionally retains tables even when they are removed from the XML, which prevents accidental data loss but also means reverting code alone cannot restore the previous schema. B