0

I am using Laravel 5.4 with PHP 7.2 all within xampp. I am trying to truncate a temporary table and then insert the new data from a MySQL View. The code executes and does exactly what I want it to do, but it says there is an error -

SQLSTATE[HY000]: General error (SQL: Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins)

Initially I had the following error General error: 1615 Prepared statement needs to be re-prepared after inserting two columns to my View: a time_start and time_end column.

I tried deleting the two columns to see if that was the issue, but now all I get in the dreaded general error.

I tried this answer Laravel: General error: 1615 Prepared statement needs to be re-prepared. It didnt work and according to the comments it will cause problem ak validations.

I have found this answer PDO error: “ SQLSTATE[HY000]: General error ” When updating database which is the exaclty the same issue to my problem, but it doesnt answer my question because I am not using fetchAll() nor am I using any duplicated variables of any variables at all.

I have tried two ways of achieving my goal as follows.

Truncate table prod_joins_temp

ProdJoinsTemp::truncate();

Then populate table prod_joins_temp from the view prod_joins

DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');

The other way which gives me exactly the same error is as follows

drop table prod_joins_temp if it exists

\Schema::dropIfExists('prod_joins_temp');

Then create table prod_joins_temp from the view prod_joins

DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');

Here is the code that makes the view

SELECT
    `scorecard54`.`production`.`date` AS `date`,
    `scorecard54`.`products`.`code` AS `code`,
    `scorecard54`.`production`.`sid_production` AS `sid`,
    `scorecard54`.`production`.`time_start` AS `time_start`,
    `scorecard54`.`production`.`time_end` AS `time_end`,
    `scorecard54`.`production`.`made` AS `made`,
    `scorecard54`.`qcontrol`.`firsts` AS `firsts`,
    `scorecard54`.`qcontrol`.`seconds` AS `seconds`,
    `scorecard54`.`production`.`broken_production` AS `p_broken`,
    `scorecard54`.`qcontrol`.`broken_qcontrol` AS `q_broken`,
    `scorecard54`.`grinding`.`discarded` AS `g_broken`,
    `scorecard54`.`products`.`prod_id` AS `prod_id`
FROM
    (
        (
            (
                `scorecard54`.`production`
            LEFT JOIN `scorecard54`.`products` ON
                (
                    (
                        `scorecard54`.`production`.`prod_code` = `scorecard54`.`products`.`prod_id`
                    )
                )
            )
        LEFT JOIN `scorecard54`.`grinding` ON
            (
                (
                    `scorecard54`.`production`.`sid_production` = `scorecard54`.`grinding`.`sid_grinding`
                )
            )
        )
    LEFT JOIN `scorecard54`.`qcontrol` ON
        (
            (
                `scorecard54`.`grinding`.`tray_id_grinding` = `scorecard54`.`qcontrol`.`tray_id_qcontrol`
            )
        )
    )
WHERE
    (
        `scorecard54`.`grinding`.`have_qcontrol` = 1
    )

And here is the function that drops and creates a table from the view

public function generateTable(Request $request) {

    // Drop temp table
    \Schema::dropIfExists('prod_joins_temp');

    // re create table with the new data
    DB::select('CREATE TABLE prod_joins_temp AS SELECT * FROM prod_joins;');


    $request->session()->flash('alert-success', 'Success: Reports table regenerated.');
    return Redirect::to('/statistics/');

}

I also tried truncating then re-populating with this function

public function generateTable(Request $request) {

    // Drop temp table
    ProdJoinsTemp::truncate();

    // re generate table with the new data
    DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins');   

    $request->session()->flash('alert-success', 'Success: Reports table regenerated.');
    return Redirect::to('/statistics/');

}

What am I doing wrong here? Is there some sort of cache for views?

warmwhisky
  • 409
  • 1
  • 10
  • 22
  • I got the prepared-statement error when I added time_start and time_end to the view, but when I deleted time_start and time_end I get this new general error. It would seem my code is in exactly the same state it was before the prepared-statement error. I have added my view code and two function I have tried to re populate the temporary table – warmwhisky Dec 17 '18 at 06:26

1 Answers1

0

I have fixed this issue from this answer SQLSTATE[HY000]: General error: 2053 error occurs at Laravel

Where before I was updating my table using the following

DB::select('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins'); 

I changed the select part of the query to update and it all works fine. please see following

DB::update('Insert Into prod_joins_temp (date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id) Select date,code,sid,time_start,time_end,made,firsts,seconds,p_broken,q_broken,g_broken,prod_id From prod_joins'); 

I don't know why the previous way worked before I added fields to my view, but now it requires me to use update instead of select

warmwhisky
  • 409
  • 1
  • 10
  • 22