You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel9/Lighthouse6中SQL语句input.前缀报错的修复方法

解决Laravel 9 + Lighthouse 6中SQL查询出现input.前缀的问题

这个错误的核心原因是你在定义验证规则、自定义查询或者Lighthouse指令时,错误地将输入字段的引用写成了input.name,而数据库中实际的列名是name,导致SQL语句把input.name当作了列名去查询。

以下是几种常见的修复场景:

1. 修正输入类型中的@unique验证规则

如果你的VoteCategoryInput里给name字段加了@unique验证,检查是否错误地指定了columns: ["input.name"],正确的写法应该直接使用数据库列名:

input VoteCategoryInput {
  name: String! 
    @rules(apply: ["required", "max:255"])
    @unique(columns: ["name"], table: "vote_categories") # 直接写列名,table可选(模型对应表名符合规范可省略)
  # 其他字段定义...
}

Lighthouse会自动将输入字段name映射到数据库的name列,不需要额外加input.前缀。

2. 修正自定义Resolver中的查询逻辑

如果是你自己编写了createVoteCategory的Resolver,检查是否在查询数据时错误地使用了input.name作为列名:

错误写法:

public function resolveCreateVoteCategory($root, array $args)
{
    // 错误:把input.name当成了数据库列名
    if (VoteCategory::where('input.name', $args['input']['name'])->exists()) {
        throw new ValidationException(...);
    }
    // ...
}

正确写法:

public function resolveCreateVoteCategory($root, array $args)
{
    // 正确:直接使用数据库列名name
    if (VoteCategory::where('name', $args['input']['name'])->exists()) {
        throw new ValidationException(Validator::make($args['input'], [
            'name' => 'unique:vote_categories'
        ]));
    }

    return VoteCategory::create($args['input']);
}

3. 检查@create指令的配置

如果使用了Lighthouse的@create自动生成突变逻辑,确保没有在指令中错误地指定带input.前缀的列名。正确的突变定义应该是:

type Mutation {
  createVoteCategory(input: VoteCategoryInput!): VoteCategory! @create
}

@create指令会自动解析input中的字段并映射到模型对应的数据库列,无需额外指定带前缀的列名。

总结一下:所有涉及数据库列的引用,都直接使用数据库实际的列名,不要加上input.前缀——input只是GraphQL突变中接收参数的容器,不是数据库的一部分。

内容的提问来源于stack exchange,提问作者mstdmstd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 12:57:24