cancel
Showing results for 
Search instead for 
Did you mean: 

my own custom table in magento database

my own custom table in magento database

How to create my own table in magento database ?

When I create my own table inside the magento database, I get an error.

Magento tables have a prefix starting with cb_ , like cb_<table_name> .

Just for testing purposes, I created a table named "scrap" and used any table, and then an error occurs when I use the command below.

 

$ ./bin/magento setup:upgrade

Updating modules:
Cache cleared successfully
Schema creation/updates:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.cb_scrap' doesn't exist, query was: SHOW CREATE TABLE `cb_scrap`

 

Isn't Magento only supposed to manage tables starting with cb_ ?
Is it possible to simply create a temporary table for test purposes that does not need to be managed by Magento without a prefix?

2 REPLIES 2

Re: my own custom table in magento database

Hello @soti 

  • Create a new module
    • First, you need to create a new module in your Magento 2 instance. You can create a new module using the Magento 2 Module Creator or manually. Once you have created a new module, you will need to create a new database schema for your module.
  • Define a database schema
    • Create a new file db_schema.xml in your module’s etc directory. This file will define the structure of your new database table.
  • The db_schema.xml file should contain the following code:
    <?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_custom_table" resource="default" engine="innodb" comment="My Custom Table">
            <column xsi:type="int" name="id" padding="10" unsigned="true" nullable="false" identity="true" comment="ID"/>
            <column xsi:type="text" name="name" nullable="false" comment="Name"/>
            <column xsi:type="text" name="description" nullable="true" comment="Description"/>
            <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
            <column xsi:type="timestamp" name="updated_at" nullable="false" default="CURRENT_TIMESTAMP" onUpdate="CURRENT_TIMESTAMP" comment="Updated At"/>
            <constraint xsi:type="primary" referenceId="PRIMARY">
                <column name="id"/>
            </constraint>
        </table>
    </schema>
  • This code creates a table named my_custom_table with five columns: id, name, description, created_at, and updated_at. The id column is the primary key, and it auto-increments.
  • Create a database schema patch
  • After defining the database schema, create a new file named InstallSchema.php in your module’s Setup directory. In this file, you will create a patch that will create the table in the Magento 2 database.
    <?php
    namespace Vendor\Module\Setup;
    use Magento\Framework\Setup\InstallSchemaInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;
            $installer->startSetup();
            if (!$installer->tableExists('my_custom_table')) {
                $table = $installer->getConnection()->newTable(
                    $installer->getTable('my_custom_table')
                )
                ->addColumn(
                    'id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary'  => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'name',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable => false'],
                    'Name'
                )
                ->addColumn(
                    'description',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    '64k',
                    [],
                    'Description'
                )
                ->addColumn(
                    'created_at',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9

Re: my own custom table in magento database

o create your own table in the Magento database, you can follow these steps:

  1. Open your database management tool, such as phpMyAdmin or MySQL Workbench.

  2. Select the database you want to create the table in.

  3. Click on the "SQL" tab to open the SQL command editor.

  4. Enter the SQL command to create your table. Here's an example SQL command to create a table called "my_custom_table" with two columns "id" and "name":

    CREATE TABLE my_custom_table ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id) );

  5. Click the "Execute" or "Go" button to execute the SQL command and create your table.