首页 > 解决方案 > 在 Postgres 中保存行时出错,如何解决?

问题描述

在我的 Laravel 5.6/PostgreSQL 10.5 应用程序中,我想将数据保存在表中:

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
)

带代码:

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);

我得到了错误:

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

为什么会出错?

我试图在我的 sql 编辑器中复制 sql 语句,并注意如下语句:

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

1) 作为字符串值输入的值没有''</p>

2)并得到错误,因为最后一个参数是整数值而不是布尔值:

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

我尝试在我的模型中添加方法:

<?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 - 是我的调试方法,看起来 getCompletedAttribute 没有被触发,因为我没有看到这个方法的 debigiing 信息。

有人可以提示为什么这个错误以及如何解决它吗?

谢谢!

标签: postgresqllaravel-5

解决方案


您的 price_total 数据类型错误

 price_total int4 NOT NULL,

应该

 price_total numeric(10,2) NOT NULL,

其中 10 是最大总位数,2 是小数点后的位数。

也可以使用money数据类型(不推荐)

 price_total money NOT NULL,

无论您做什么,都不要使用任何类型的浮点数。


推荐阅读