0

I have a custom table created through db_schema.xml (code below):

<?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="Magebit_Faq_Question">
        <column xsi:type="int" name="id" unsigned="true" nullable="false" padding="10" identity="true" comment="id"/>
        <column xsi:type="text" name="question" nullable="false" comment="Question"/>
        <column xsi:type="text" name="answer" nullable="false" comment="Answer"/>
        <column xsi:type="smallint" name="status" nullable="false" default="0" comment="Status"/>
        <column xsi:type="int" name="position" nullable="false" default="0" comment="Position"/>
        <column xsi:type="timestamp" name="updated_at" on_update="true" default="CURRENT_TIMESTAMP" comment="Time of event"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
    </table>
</schema>

It is clear here that the primary key is id so, technically, the id field should be auto_increment. But, When I run the query below

INSERT INTO Magebit_Faq_Question(question, answer, status, position) VALUE ('what?', 'just what', 1, 0);

I get an error saying

[2022-08-09 14:32:06] [HY000][1364] Field 'id' doesn't have a default value

How can I solve this issue?

1 Answer 1

0

Like you say, the id is clearly a primary key but you have not checked if it is the case as of yet:

  • I suggest first to perform the following query: describe Magebit_Faq_Question.

See below a screenshot of a table I made with a similar schema to yours

enter image description here

If you don't see the auto-increment in the result for this query, then we have an answer to your question.

1
  • Thank you. Once I ran describe Magebit_Faq_Question, I see that the id is not auto increment. After running ALTER TABLE Magebit_Faq_Question MODIFY id INTEGER NOT NULL AUTO_INCREMENT; The id field is now auto_increment.
    – LosMos
    Commented Aug 10, 2022 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.