Home Blog Contact

How to create a database table in Shopware 6?

Jan 27th 2022

Let's create a table in the database. If you don't know what migrations are, please check this blogpost.

php bin/console database:create-migration -p MatheusGontijoHelloWorld --name CreateNewTable


Creating migration...
Creating plugin-migration with namespace MatheusGontijo\HelloWorld\Migration in path custom/plugins/MatheusGontijoHelloWorld/src/Migration...
Creating core-migration ...
Migration created: "custom/plugins/MatheusGontijoHelloWorld/src/Migration/Migration1642875903CreateNewTable.php"

Please, add our SQL commands to create tables in the database. Edit the created file. In my example it's named Migration/Migration1642875903CreateNewTable.php:

<?php declare(strict_types=1);

namespace MatheusGontijo\HelloWorld\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1642875903CreateNewTable extends MigrationStep
    public function getCreationTimestamp(): int
        return 1642875903;

    public function update(Connection $connection): void
        $query = <<<SQL
        CREATE TABLE IF NOT EXISTS `matheusgontijo_conference` (
          `id` binary(16) NOT NULL,
          `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
          `location` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
          `created_at` datetime(3) DEFAULT NULL,
          `updated_at` datetime(3) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;


    public function updateDestructive(Connection $connection): void

Run the following command to execute the migration:

php bin/console database:migrate MatheusGontijoHelloWorld --all


Get collection for identifier: "MatheusGontijoHelloWorld"
migrate Migrations
 1/1 [============================] 100%

 ---------- ----------------------
  Action     Number of migrations
 ---------- ----------------------
  Migrated   1 out of 1
 ---------- ----------------------

all migrations for identifier: "MatheusGontijoHelloWorld" executed
cleared the shopware cache

If everything went all right, you are able to see the the table created in the database!

That's was easy! Have a great day!