Magento

[Magento] magento 2 How to Create a new Database Table

chsr 2023. 6. 29. 14:55
728x90
반응형

 Magento 2 How to Create a new Database Table 

db_schema.xml 파일을 사용하여 신규 데이터베이스 테이블 생성


How to create

1. app/code/<CompanyName>/<ModuleName>/etc/db-schema.xml 파일 생성

<?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="new_table_name" resource="default" engine="innodb" comment="new table purpose of use">
        <column xsi:type="int" name="entity_id" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
        <column xsi:type="int" name="status" padding="11" unsigned="false" nullable="false" default="0" comment="Status"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="true" default="CURRENT_TIMESTAMP"
                comment="Created At"/>
        <column xsi:type="timestamp" name="updated_at" on_update="true" nullable="true" default="CURRENT_TIMESTAMP"
                comment="Updated At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="entity_id" />
        </constraint>
    </table>
</schema>

2. 하기 명령어 실행하여 db_schema_whitelist.json 파일 추가 (하기 명령어 실행 시 json 파일이 자동으로 생성됨)

php bin/magento setup:db-declaration:generate-whitelist --module-name=CompanyName_ModuleName

3. json 파일 생성되면 하기 명령어 순차적으로 실행

php bin/magento s:up && php bin/magento se:s:d -f

 

 

728x90
반응형

'Magento' 카테고리의 다른 글

[Magento] magento 2 available conditions type  (0) 2023.06.19
[Magento] magento 2 Run cron  (1) 2023.03.23
[Magento] 마젠토(Magento) 정보 1  (2) 2021.07.14