Postgresql 在Postgres中保存行时出错,如何修复?
在我的Laravel 5.6/PostgreSQL 10.5应用程序中 我想在表中保存数据:Postgresql 在Postgres中保存行时出错,如何修复?,postgresql,laravel-5,Postgresql,Laravel 5,在我的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 i
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) 作为字符串值输入的值不带“”
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;
}
您的价格总额的数据类型错误
price_total int4 NOT NULL,
应该是
price_total numeric(10,2) NOT NULL,
其中10为最大总位数,2为小数点后的位数
您也可以使用货币数据类型(不推荐)
无论您做什么,都不要使用任何类型的浮动。您的价格总额的数据类型是错误的
price_total int4 NOT NULL,
应该是
price_total numeric(10,2) NOT NULL,
其中10为最大总位数,2为小数点后的位数
您也可以使用货币数据类型(不推荐)
无论您做什么,都不要使用任何类型的浮点。很明显,3500.75
不能存储在整数中,因为整数不允许小数。您可能希望将price\u total
定义为decimal(12,2)
或类似的内容。布尔类型的有效常量为true
或false
——不能将数字存储在定义为布尔值的列中。显然3500.75
不能存储在整数中,因为整数不允许小数。您可能希望将price\u total
定义为decimal(12,2)
或类似的内容。布尔类型的有效常量是true
或false
-不能将数字存储在定义为布尔值的列中money
有几个问题-我永远不会使用它。我也不会。我想你可以乘/除100,然后使用整数,但是我发现这也是一个头疼的问题。钱
有几个问题-我永远不会使用它。我也不会。我想你可以乘/除100,然后使用整数,但我发现这也是一个头疼的问题。