xpertbot / craft-wheelform

Craft CMS 4 Form with Database integration
MIT License
66 stars 32 forks source link

Install issue (on MySQL Galera cluster setup) #301

Closed holiday-jan closed 5 months ago

holiday-jan commented 9 months ago

Hi!

During the ./craft plugin/install process, we receive the following issue:

insert into {{%wheelform_forms}} ... done (time: 0.012s)

insert into {{%wheelform_form_fields}} ...Exception: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (cr_demo.wheelform_form_fields, CONSTRAINT fk_rqhzcqwspclgbchelqochlustfsetbvaadxc FOREIGN KEY (form_id) REFERENCES wheelform_forms (id) ON DELETE CASCADE) The SQL being executed was: INSERT INTO wheelform_form_fields (form_id, type, name, order, required, dateCreated, dateUpdated, uid) VALUES (1, 'email', 'email', 1, 1, '2023-12-07 12:37:44', '2023-12-07 12:37:44', '5389955c-91c0-4d12-b1e1-ccf4cdc831d3') (/data/www/host/vendor/yiisoft/yii2/db/Schema.php:676)`

Extra information: our DB-setup is on a galera-cluster and, the first "auto ID" for the initial insert was 3, instead of the expacted 1. image So the second insert, where the form_id value is set to 1, fails the constraint.

Hope this imformation helps you to make the install process smoother.

Best regards, Jan

xpertbot commented 9 months ago

Thank you for the bug report. That's a very specific bug. I'll look into it.

holiday-jan commented 8 months ago

Hi,

Maybe this is a suitable solution, if you change the function insertDefaultData() in the Install.php script to:

    protected function insertDefaultData()
    {
        $firstForm = new \wheelform\db\Form();
        $firstForm->site_id = Craft::$app->sites->currentSite->id;
        $firstForm->name = 'Contact Form';
        $firstForm->to_email = "user@example.com";
        $firstForm->active = 1;
        $firstForm->send_email = 1;
        $firstForm->recaptcha = 0;

        if ($firstForm->save()) {
            $this->insert(
                '{{%wheelform_form_fields}}',
                [
                    "form_id" => $firstForm->id,
                    "type" => 'email',
                    "name" => "email",
                    "order" => 1,
                    "required" => 1,
                ]
            );
            $this->insert(
                '{{%wheelform_form_fields}}',
                [
                    "form_id" => $firstForm->id,
                    "type" => 'text',
                    "name" => "name",
                    "order" => 2,
                    "required" => 0,
                ]
            );
            $this->insert(
                '{{%wheelform_form_fields}}',
                [
                    "form_id" => $firstForm->id,
                    "type" => 'text',
                    "name" => "message",
                    "order" => 3,
                    "required" => 1,
                ]
            );
        }
    }

!! Untested, only as example !!

Hope this will help you in a good and solid solution. We love your plugin, thank you for your time again!

Beste regards, Jan

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

holiday-jan commented 5 months ago

I tested my suggested solution: https://github.com/xpertbot/craft-wheelform/issues/301#issuecomment-1889414754, and it works prefectly both ways (normal DB-setup and our cluster setup). Can you please take another look into this. It would realy help us forward. Best regards, Jan

xpertbot commented 5 months ago

I'll take another look and deploy a fix if appropriate.

holiday-jan commented 5 months ago

Any progress on this?

I tested the code above both ways, and it works as expected. So if you please could consider a fix, it would helps us in the future with new projects.