【发布时间】:2021-08-29 02:02:28
【问题描述】:
所以我在数据库 sqlserver 上插入数据时遇到了问题。 id sqlserver 拒绝插入,因为数据库要自己插入 id。
我必须尝试像这样设置 IDENTITY_INSERT :
DB::statement('SET IDENTITY_INSERT articles ON;');
并将 IDENTITY_INSERT 设置为 OFF,如下所示:
DB::statement('SET IDENTITY_INSERT articles OFF;');
但这不适用于我的代码。
public function store(Request $request){
//to sparate to methode
$article = $request->isMethod('put') ? Article::findOrFail($request->article_id) : new Article;
$article->id = $request->input('article_id');
$article->title = $request->input('title');
$article->body = $request->input('body');
DB::statement('SET IDENTITY_INSERT articles ON;');
if($article->save()){
return new ArticleResources($article);
}
DB::statement('SET IDENTITY_INSERT articles OFF;');
}
这个错误看起来像这样:
SQLSTATE[23000]:[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]当 IDENTITY_INSERT 设置为 OFF 时,无法为表“articles”中的标识列插入显式值。
【问题讨论】: