mardi 11 septembre 2018

Error saving row in Postgres and how to fix it?

In my Laravel 5.6/PostgreSQL 10.5 application I want to save data in table :

CREATE TABLE public.rt_orders (
    id serial NOT NULL,
    user_id int4 NULL,
    card_owner varchar(100) NOT NULL,
    discount int4 NULL DEFAULT 0,
    discount_code varchar(255) NULL,
    qty_count int4 NOT NULL,
    price_total int4 NOT NULL,
    payment varchar(255) NOT NULL,
    completed bool NOT NULL DEFAULT false,
    error_message varchar(255) NULL,
    created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT rt_orders_pkey PRIMARY KEY (id),
    CONSTRAINT orders_user_id_foreign FOREIGN KEY (user_id) REFERENCES rt_users(id) ON UPDATE CASCADE ON DELETE SET NULL
)

with code :

try {
    DB::beginTransaction();
    $insertOrderData= [
        'user_id'=> $loggedUser->id,
        'card_owner'=> $card_owner,
        'qty_count'=> Cart::instance('default')->count(),
        'price_total'=> Cart::instance('default')->subtotal(),
        'payment'=> 'stripe',
        'completed'=> true
    ];
    $newOrder                    = Order::create($insertOrderData);

and I got error:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "3500.75" (SQL: insert into "rt_orders" ("user_id", "card_owner", "qty_count", "price_total", "payment", "completed") values (5, gdfgdfgds, 2, 3500.75, stripe, 1) returning "id") {"userId":5,"email":"admin@mail.com","exception":"[object] (Illuminate\\Database\\QueryException(code: 22P02): SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: \"3500.75\" (SQL: insert into \"rt_orders\" (\"user_id\", \"card_owner\", \"qty_count\", \"price_total\", \"payment\", \"completed\") values (5, gdfgdfgds, 2, 3500.75, stripe, 1) returning \"id\") at /mnt/_work_sdb8/wwwroot/lar/ArtistsRating/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 22P02): SQLSTATE[22P02]: I

Why error ?

I tried to copy the sql statement in my sql editor and payed attention that a statement like :

insert into "rt_orders" ("user_id", "card_owner", "qty_count", "price_total", "payment", "completed") values (5, fsdf, 2, 3500.75, stripe, 1) 

1) Values entered as string values are without ‘’

2) and got error as last parameter was integer value not boolean:

SQL Error [42804]: ERROR: column "completed" is of type boolean but expression is of type integer
  Hint: You will need to rewrite or cast the expression.
  Position: 149

I tried in my model to add method:

<?php
namespace App;

use DB;
use App\MyAppModel;
use App\User;
use App\SongOrder;

class Order extends MyAppModel
{

    protected $table = 'orders';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected static function boot() {
        parent::boot();
    }

    protected $fillable = ['user_id', 'card_owner', 'discount', 'discount_code', 'price_total', 'qty_count', 'price_total', 'payment', 'completed', 'error_message'];


    public function getCompletedAttribute($value)
    {
        $this->debToFile(print_r($value,true),' 000 getCompletedAttribute  -7 $value::');
        $ret= (int)$value == 1;
        $this->debToFile(print_r($ret,true),' 000 getCompletedAttribute  -7 $ret::');
        return $ret;
    }

debToFile - is my debugging method and looks like the getCompletedAttribute is not triggered as I do not see my debigiing info of this method.

Can somebody give a hint why this error and how to fix it?

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2x57Te5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire