【问题标题】:PostgreSQL Insert error on a not null ValuePostgreSQL 在非空值上插入错误
【发布时间】:2016-06-21 16:11:51
【问题描述】:

尝试在 PostgreSQL 数据库上执行插入查询时遇到一个奇怪的错误。我正在执行以下查询:

insert into "shopping_carts" 
(
"application_version", "charges", "device_id", "device_type", "id", "new_client",
"os_version", "platform_name", "resolution", "shopping_cart_date",
"shopping_cart_date_day", "shopping_cart_date_day_of_week",
"shopping_cart_date_day_of_year", "shopping_cart_date_hour",
"shopping_cart_date_minute", "shopping_cart_date_month",
"shopping_cart_date_numeric", "shopping_cart_date_quarter", 
"shopping_cart_date_quarter_name", "shopping_cart_date_second", 
"shopping_cart_date_week_of_year", "shopping_cart_date_year", "status", 
"store_id", "store_name", "tip", "total_after_discounts", "total_discount", 
"total_items", "user_gender", "user_id", "whim_has_photo", "whim_what", 
"whim_where"
) 
values 
(
2.0, 0.0, '105755B41135E2A6F5C09089EE776BA179A4DD290E27B6DC11AFC94534572BD7', 
'smartphone', 'QWFISFJ23434', true, 'Ubuntu 15.10', 'ios', '1920x1080', 
'2016-06-20 18:42:15', 20, 1, 171, 18, 42, 6, 1466448135, 2, 'Second Quarter', 
15, 25, 2016, 'PROGRESS', 15, 'BOG F. Centro Andino', 0, 0, 0, 0, 'M', 32, 
TRUE, 'Lechona', 'Lechoneria de la esquina'
)

错误是:

Kernel error: ERROR:  null value in column "tip" violates not-null constraint

DETAIL:  Failing row contains (QWFISFJ23434, 0, 0, 0, PROGRESS, null, null, 0, 15, BOG F. Centro Andino, null, null, null, 32, M, Ubuntu 15.10, 1920x1080, 2.0, ios, 105755B41135E2A6F5C09089EE776BA179A4DD290E27B6DC11AFC94534572BD7, smartphone, 2016-06-20 18:42:15, 1466448135, 2016, 6, 20, 2, Second Quarter, 171, 1, 25, 18, 42, 15, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, t, null, null, null, null, null, null).

我显然是在为“tip”列发送一个值,所以我不明白为什么会出现这个错误。

此外,还有一些可能有用的附加信息:

  1. 在错误详细信息中,有一个奇怪的“t”。
  2. shopping_carts 表有更多列,但它们都可以为空。
  3. 在数据库中存在 3 个表,它们继承自 shopping_carts 表,并在 shopping_carts 表中具有插入规则。
  4. 我正在使用 laravel 迁移来创建数据库

感谢您能给我的任何帮助。

【问题讨论】:

  • 这是在您为数据库播种时发生的,还是在正常使用时发生的?您用于插入行的确切代码是什么?
  • 另外,您能告诉我们该表的完整定义吗?
  • 那个't'true的另一种表示方式:postgresql.org/docs/current/static/datatype-boolean.html

标签: php postgresql laravel laravel-5 lumen


【解决方案1】:

找到了解决办法。

我在 shopping_cart 子表上有一个插入规则。问题是我在定义的规则之后添加了“提示”列。

我不得不再次执行规则查询来更新它们。这样就解决了问题。

【讨论】:

    【解决方案2】:

    我的问题是模型没有可填充的值。
    这可能发生在 mass assignable 操作中。
    只需在模型中添加它(在您的情况下为“shopping_cart”): /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'column0', 'column1', 'add the problematic one here' ];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 2014-05-05
      • 2019-08-08
      • 1970-01-01
      • 2013-02-20
      相关资源
      最近更新 更多