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?
Hello @soti
<?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>
<?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
o create your own table in the Magento database, you can follow these steps:
Open your database management tool, such as phpMyAdmin or MySQL Workbench.
Select the database you want to create the table in.
Click on the "SQL" tab to open the SQL command editor.
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) );
Click the "Execute" or "Go" button to execute the SQL command and create your table.