1

I have event_id column in my sales_order table. I want to place an order & I am not sending event_id because its zero by default. When am trying to place an order, Its throwing me the error Column not found: 1054 Unknown column 'so.event_id' in 'where clause', query was: INSERT INTO sales_order ...

enter image description here

Here is the error am getting in INSERT Query:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'event_id' in 'where clause', query was: INSERT INTO `sales_order` (`state`, `status`, `protect_code`, `shipping_description`, `is_virtual`, `store_id`, `customer_id`, `base_discount_amount`, `base_grand_total`, `base_shipping_amount`, `base_shipping_tax_amount`, `base_subtotal`, `base_tax_amount`, `base_to_global_rate`, `base_to_order_rate`, `discount_amount`, `grand_total`, `shipping_amount`, `shipping_tax_amount`, `store_to_base_rate`, `store_to_order_rate`, `subtotal`, `tax_amount`, `total_qty_ordered`, `customer_is_guest`, `customer_note_notify`, `customer_group_id`, `quote_id`, `base_shipping_discount_amount`, `base_subtotal_incl_tax`, `base_total_due`, `shipping_discount_amount`, `subtotal_incl_tax`, `total_due`, `weight`, `increment_id`, `applied_rule_ids`, `base_currency_code`, `customer_email`, `customer_firstname`, `customer_lastname`, `customer_middlename`, `discount_description`, `global_currency_code`, `order_currency_code`, `shipping_method`, `store_currency_code`, `store_name`, `total_item_count`, `discount_tax_compensation_amount`, `base_discount_tax_compensation_amount`, `shipping_discount_tax_compensation_amount`, `base_shipping_discount_tax_compensation_amnt`, `shipping_incl_tax`, `base_shipping_incl_tax`, `gift_message_id`, `has_ambassador`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

I have already run all cache & upgrade commands many times but I don't know why its doing this. Here is my code:

public function createMageOrder($orderData) {
        $store = $this->_storeManager->getStore();
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($orderData["entity"]["customer_email"]); // load customet by email address
        if (!$customer->getEntityId()) {
            echo "Customer does not exist. please create here...";
            exit;
            //If not avilable then create this customer 
            $customer->setWebsiteId($websiteId)
                    ->setStore($store)
                    ->setFirstname($orderData['shipping_address']['firstname'])
                    ->setLastname($orderData['shipping_address']['lastname'])
                    ->setEmail($orderData['email'])
                    ->setPassword($orderData['email']);
            $customer->save();
        }
        $quote = $this->quote->create(); //Create object of quote
        $quote->setStore($store); //set store for which you create quote
        // if you have allready buyer id then you can load customer directly 
        $customer = $this->customerRepository->getById($customer->getEntityId());
        $quote->setCurrency();
        $quote->assignCustomer($customer); //Assign quote to customer
        //add items in quote
        foreach ($orderData["entity"]["items"] as $item) {
            $product = $this->_product->load($item['product_id']);
            $product->setPrice($item['price']);
            $quote->addProduct(
                    $product, intval($item['qty_ordered'])
            );
        }

        //Set Address to quote
        $quote->getBillingAddress()->addData($orderData["entity"]["billing_address"]);
        $quote->getShippingAddress()->addData($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]);

        // Collect Rates and Set Shipping & Payment Method
        $shippingAddress = $quote->getShippingAddress();
        $shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates()
                ->setShippingMethod($orderData["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"]); //shipping method
        $quote->setPaymentMethod($orderData["entity"]["payment"]["method"]); //payment method
        $quote->setInventoryProcessed(false); //not effetc inventory
        $quote->save(); //Now Save quote and your quote is ready
        // Set Sales Order Payment
        $quote->getPayment()->importData(['method' => $orderData["entity"]["payment"]["method"]]);
//        $quote->setData('event_id', $orderData["entity"]["event_id"]);
        // Collect Totals & Save Quote
        $quote->collectTotals()->save();

        // Create Order From Quote
        $order = $this->quoteManagement->submit($quote);

        $order->setEmailSent(0);
        $increment_id = $order->getRealOrderId();
        if ($order->getEntityId()) {
            $result['order_id'] = $order->getRealOrderId();
        } else {
            $result = ['error' => 1, 'msg' => 'Your custom message'];
        }
        return $result;
    }

I am calling this function in placeGTOrder($post) function where $post is my $orderData. Here is the function on which am sending my request as an action:

public function placeGTOrder($post) {
        $formData = $this->_helper->unserializeForm($post['formData']);
        $order = array();

        //fill cart items to order items 
        $cart = $this->_objectManager->get('\Magento\Checkout\Model\Cart');
        $itemsCollection = $cart->getQuote()->getItemsCollection();
        $itemsVisible = $cart->getQuote()->getAllVisibleItems();
        $items = $cart->getQuote()->getAllItems();
        $getItems = [];
        foreach ($items as $item) {
            $collect = array();
            $collect["base_original_price"] = 45;
            $collect["base_price"] = 45;
            $collect["base_price_incl_tax"] = 45;
            $collect["base_row_invoiced"] = 0;
            $collect["base_row_total"] = 45;
            $collect["base_tax_amount"] = 0;
            $collect["base_tax_invoiced"] = 0;
            $collect["discount_amount"] = 4.5;
            $collect["discount_percent"] = 10;
            $collect["free_shipping"] = 0;
            $collect["is_virtual"] = 0;
            $collect["name"] = $item->getName();
            $collect["original_price"] = 45;
            $collect["price"] = 45;
            $collect["price_incl_tax"] = 45;
            $collect["product_id"] = $item->getProductId();
            $collect["product_type"] = "simple";
            $collect["qty_ordered"] = $item->getQty();
            $collect["row_total"] = 45;
            $collect["row_total_incl_tax"] = 45;
            $collect["sku"] = $item->getSku();
            $collect["store_id"] = 1;
            $getItems[] = $collect;
        }


//        $order["entity"]["event_id"] = $formData["eventID"];
        $order["entity"]["base_currency_code"] = "USD";
        $order["entity"]["base_discount_amount"] = -4.5;
        $order["entity"]["base_grand_total"] = 45.5;
        $order["entity"]["base_shipping_amount"] = 5;
        $order["entity"]["base_subtotal"] = 45;
        $order["entity"]["base_tax_amount"] = 0;
        $order["entity"]["customer_email"] = urldecode($formData['Email']);
        $order["entity"]["customer_firstname"] = $formData['FirstName'];
        $order["entity"]["customer_group_id"] = 3;
        $order["entity"]["customer_id"] = $formData['selected_customer_id'];
        $order["entity"]["customer_is_guest"] = 0;
        $order["entity"]["customer_lastname"] = $formData['LastName'];
        $order["entity"]["customer_note_notify"] = 1;
        $order["entity"]["discount_amount"] = -4.5;
        $order["entity"]["email_sent"] = 1;
        $order["entity"]["coupon_code"] = "";
        $order["entity"]["discount_description"] = "Test1";
        $order["entity"]["grand_total"] = 45.5;
        $order["entity"]["is_virtual"] = 0;
        $order["entity"]["order_currency_code"] = "USD";
        $order["entity"]["shipping_amount"] = 5;
        $order["entity"]["shipping_description"] = "Flat Rate - Fixed";
        $order["entity"]["state"] = "new";
        $order["entity"]["status"] = "pending";
        $order["entity"]["store_currency_code"] = "USD";
        $order["entity"]["store_id"] = 1;
        $order["entity"]["store_name"] = "Main Website\nMain Website Store\n";
        $order["entity"]["subtotal"] = 45;
        $order["entity"]["subtotal_incl_tax"] = 45;
        $order["entity"]["tax_amount"] = 0;
        $order["entity"]["total_item_count"] = 1;
        $order["entity"]["total_qty_ordered"] = 1;
        $order["entity"]["weight"] = 1;
        $order["entity"]["items"] = $getItems;
        $order["entity"]["billing_address"]["address_type"] = "billing";
        $order["entity"]["billing_address"]["city"] = urldecode($formData['MainAddress.City']);
        $order["entity"]["billing_address"]["company"] = "";
        $order["entity"]["billing_address"]["country_id"] = urldecode($formData["MainAddress.Country"]);
        $order["entity"]["billing_address"]["email"] = urldecode($formData['Email']);
        $order["entity"]["billing_address"]["firstname"] = urldecode($formData['FirstName']);
        $order["entity"]["billing_address"]["lastname"] = urldecode($formData['LastName']);
        $order["entity"]["billing_address"]["postcode"] = urldecode($formData['MainAddress.Zip']);
        $order["entity"]["billing_address"]["region"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_code"] = urldecode($formData['region']);
        $order["entity"]["billing_address"]["region_id"] = 19;
        $order["entity"]["billing_address"]["street"] = array(urldecode($formData["MainAddress.Address1"]), urldecode($formData["MainAddress.Address2"]));
        $order["entity"]["billing_address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["payment"]["method"] = "checkmo";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["address_type"] = "shipping";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["city"] = urldecode($formData["MailingAddress.City"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["company"] = "";
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["country_id"] = urldecode($formData["MailingAddress.Country"]);
//        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["customer_address_id"] = 2;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["email"] = urldecode($formData["Email"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["firstname"] = urldecode($formData["FirstName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["lastname"] = urldecode($formData["LastName"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["postcode"] = urldecode($formData["MainAddress.Zip"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_code"] = urldecode($formData["region"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["region_id"] = 19;
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["street"] = array(urldecode($formData["MailingAddress.Address1"]), urldecode($formData["MailingAddress.Address2"]));
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["address"]["telephone"] = urldecode($formData["PrimaryPhone"]);
        $order["entity"]["extension_attributes"]["shipping_assignments"]["shipping"]["method"] = "flatrate_flatrate";

        $result = $this->createMageOrder($order);
        echo json_encode($result); exit; 
//        $ApiResponse = $this->_helper->placeOrderByAPI($order);
//        print_r($ApiResponse);
        exit;
        exit;
    }

Here is the table: enter image description here

Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • Please check your `sales_order` table to see if there is in fact a event_id column. If there isn't one your UpgradeSchema scripts may not have worked and you will need to create an InstallSchema or UpgradeSchema to install the column. – giolliano sulit Mar 26 '19 at 11:00
  • Already have installData & Recurring files to upgradeschema in db. also event_id column is exists in the sales_order table as well. – Abdulrehman Sheikh Mar 26 '19 at 11:03
  • Can you show us a screenshot of your sales_order table – giolliano sulit Mar 26 '19 at 11:04
  • I have updated my question please review the last image. event_id is already exists in the table – Abdulrehman Sheikh Mar 26 '19 at 11:10
  • I am confused I am not inserting event id then why its considering it? normally insert query just insert records without checking the all columns. – Abdulrehman Sheikh Mar 26 '19 at 11:10
  • Hmm, is event_id a required field? – giolliano sulit Mar 26 '19 at 11:14
  • No its not required field. Already orders are working perfect without event id but when I am trying to place an order programatically its not working. – Abdulrehman Sheikh Mar 26 '19 at 11:42
  • Where is the event_id set? So that the query knows where to put it. Does `so` refer to sales_order or another table? You haven't included the full query that's failing either. – Mark Rees Mar 26 '19 at 19:23
  • I have not written any query so how can i show you complete query ? Review my posted images as well you will see query in error log screenshot. Its just an Insert query but its giving me an error for where clause how is it possible? – Abdulrehman Sheikh Mar 27 '19 at 06:29
  • Can you c/p (and edit question with that) line 2 or line 5 from error? – Tpojka Mar 28 '19 at 08:01
  • @Tpojka I have modified my question. Please review it. – Abdulrehman Sheikh Mar 28 '19 at 08:14
  • If you check columns (in parenthesis) you'll see there is no `event_id` column - as error provides. – Tpojka Mar 28 '19 at 08:18
  • No, I have tried to add event_id column and value as well but it always shows the same error. I have tried to run this insert query directly in the phpmyadmin database sql engine but it always throwing the same error message. – Abdulrehman Sheikh Mar 28 '19 at 08:19
  • Any reason I can remember now (that possibly could make you issue with this table) is existence of trigger related to insert into that table. Check in DB if there is trigger that contains `BEFORE INSERT ON sales_order` and/or `AFTER INSERT ON sales_order`. But regardless, you have to get correct query in PHPMyAdmin first. I really don't see why it wouldn't work as plain insert. – Tpojka Mar 28 '19 at 08:25
  • Same with me, Its a very ambiguous behaviour. I have never faced this before. The query is just an insertion query which should must run and insert data. There is no where clause and the column event_id is also exists. event_id column have default value null so It should never behave like this. – Abdulrehman Sheikh Mar 28 '19 at 08:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190822/discussion-between-abdulrehman-sheikh-and-tpojka). – Abdulrehman Sheikh Mar 28 '19 at 09:43

1 Answers1

1

Your query is perfect, You just need to check triggers in your database, if there is any trigger running then disable it or remove it then again run your code. I hope that will work.

Here is the Query:

SHOW triggers;
Farhan Ali
  • 230
  • 3
  • 14
  • OMG! I have just runn the show triggers; query and there are 4 Trigers which are firing before order insert. Thats why they are creating issues and not allowing me to insert order programatically. Thats really appriciateable. Thank you very much. Why I didnt think out of the box like you. Thanks for your help and efforts. – Abdulrehman Sheikh Mar 28 '19 at 12:57