我正在尝试创建模型的新实例,并在其中创建其他两个模型的实例。我的架构如下:
extend type Mutation @middleware(checks: ["auth:api"]) {
createProduct(input: CreateProductInput! @spread): Product @create
}
"TYPES"
type Product {
id: ID!
name: String!
sku: String!
manufacturer: ProductManufacturer! @belongsTo
category: ProductCategory! @belongsTo
}
type ProductManufacturer {
id: ID!
name: String!
products: [Product!]! @hasMany
}
type ProductCategory {
id: ID!
name: String!
products: [Product!]! @hasMany
}
"INPUTS"
input CreateProductInput {
name: String!
sku: String!
manufacturer: CreateProductManufacturerRelation!
category: CreateProductCategoryRelation!
}
input CreateProductManufacturerRelation {
connect: ID
create: CreateProductManufacturerInput
}
input CreateProductCategoryRelation {
connect: ID
create: CreateProductCategoryInput
}
input CreateProductManufacturerInput {
name: String!
}
input CreateProductCategoryInput {
name: String!
}
如果我对制造商和类别都使用connect:
,则效果很好。我也将connect:
用于另一个,将create:
用于另一个。
如果我尝试同时create:
和制造商和类别,突变将失败,并出现以下错误。 (下面的查询和错误)
mutation {
createProduct(input: {
name:"My amazing product"
sku:"SKU0001"
manufacturer: {
create: {
name: "Awesome Corp"
}
}
category: {
create: {
name: "Doodads"
}
}
}){
id
}
}
"debugMessage": "SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (SQL: insert into `products` (`name`, `sku`, `manufacturer_id`) values (My amazing product, SKU0001, 21))"
因此,看起来它甚至都没有尝试创建新类别。奇怪的是,如果我在CreateProductInput
中翻转制造商的顺序和类别关系,则会发生相反的情况,并且不会创建制造商。
我在这里做错什么了吗?