通过关系结果保存新记录无法将值NULL插入列';created_at';


Save new record trough relationship results in Cannot insert the value NULL into column 'created_at'

我有两个具有多对多关系的模型,当它尝试检索数据时,一切都很正常,因此关系正常工作。但当我试图通过关系保存新数据时,我会得到

SQLSTATE[23000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot insert the value NULL into column 'created_at', table 'dlist.dbo.dlist_employee'; column does not allow nulls. INSERT fails. (SQL: insert into [dlist_employee] ([dlist_id], [employee_id]) values (2, 26))

这是我的代码:

   $new_employee = new Employee(array('name' => 'name','username' => 'username','email' => 'email'));
   $dlist = Dlist::find(2)->employees()->save($new_employee);

根据我的理解,created_at和updated_at字段是自动填充的。既然我可以成功地提取数据(如果关系定义正确),我在这里做错了什么?

看起来保存脚本正试图在数据库表的created_at列中插入一个"null"。

尝试将数据库中的"created_at"列设置为"nullable"。

请检查:这与时间戳有关

您的型号有public $timestamps = false;吗?如果是,那么您需要手动处理时间戳

还要检查迁移(如果是您用来创建表的系统)是否具有created_at updated_at 的正确列定义