gpt4 book ai didi

python - 意外错误 : WHERE parameter "undefined" has invalid "undefined" value

转载 作者:行者123 更新时间:2023-12-01 06:51:53 26 4
gpt4 key购买 nike

我正在尝试为我的一个使用 SQLAlchemy 开发的项目实现森林管理。我已经按照以下结构定义了我的模型:

SupplierCategory = sqlalchemy.Table(
‘supplier_category’, Base.metadata,
Column(‘supplier_id’, UUID(as_uuid=True), sqlalchemy.ForeignKey(‘supplier.id’)),
Column(‘category_id’, UUID(as_uuid=True), sqlalchemy.ForeignKey(‘category.id’))
)

class Supplier(Base):
id = Column(UUID(as_uuid=True), primary_key=True, unique=True, default=uuid4, server_default=text(‘uuid_generate_v4()’))
name = Column(String)
categories = relationship(“Category”, secondary=SupplierCategory, back_populates=“suppliers”)

class Category(Base):
id = Column(UUID(as_uuid=True), primary_key=True, unique=True, default=uuid4, server_default=text(‘uuid_generate_v4()’))
name = Column(String)
suppliers = relationship(“Supplier”, secondary=SupplierCategory, back_populates=“categories”)

当我从林管理导航到供应商并尝试为 seller_categories 创建条目时,我遇到一条错误消息,

意外错误:WHERE 参数“未定义”具有无效的“未定义”值

我真的不知道如何解决这个问题。我真的需要一些帮助。

<小时/>

models/supplier.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Model = sequelize.define('supplier', {
id: {
type: DataTypes.UUID,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
},
}, {
tableName: 'supplier',
underscored: true,
schema: process.env.DATABASE_SCHEMA,
});

Model.associate = (models) => {
Model.hasMany(models.supplier_category, {
foreignKey: {
name: 'supplier_id',
},
onDelete: 'cascade',
hooks: true
});
};

return Model;
};

models/category.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Model = sequelize.define('category', {
id: {
type: DataTypes.UUID,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
},
}, {
tableName: 'category',
underscored: true,
schema: process.env.DATABASE_SCHEMA,
});

Model.associate = (models) => {
Model.hasMany(models.supplier_category, {
foreignKey: {
name: 'category_id',
},
onDelete: 'cascade',
hooks: true
});
};

return Model;
};

models/supplier_category.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Model = sequelize.define('supplier_category', {
}, {
tableName: 'supplier_category',
underscored: true,
timestamps: false,
schema: process.env.DATABASE_SCHEMA,
});

Model.removeAttribute('id');

Model.associate = (models) => {
Model.belongsTo(models.supplier, {
foreignKey: {
name: 'supplier_id',
},
as: 'supplier',
});
Model.belongsTo(models.category, {
foreignKey: {
name: 'category_id',
},
as: 'category',
});
};

return Model;
};

Screenshot of the error

以下是从控制台发送到服务器的请求负载:

{
"data": {
"relationships": {
"category": {
"data": {
"type": "categories",
"id": "358fedc9-05a3-4174-a6a1-501aafa750ab"
}
},
"supplier": {
"data": {
"type": "suppliers",
"id": "e454e11a-2afb-49b4-b163-fed7a7f54a7f"
}
}
},
"type": "supplier_categories"
}
}

服务器返回错误,状态代码为 500。

最佳答案

suppliercategory 之间的关系是通过连接表 SupplierCategory 定义的。

Forest Admin 已插入您的 SQL 数据库并使用 Express/Sequelize 为您提供管理 API(如果您使用 Lumber 安装)。

因此,就您而言,您必须使用 belongsToMany 关联。请参阅here获取链接。

您的模型应如下所示:

models/supplier.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Supplier = sequelize.define('supplier', {
name: {
type: DataTypes.STRING,
},
}, {
tableName: 'supplier',
timestamps: false,
schema: process.env.DATABASE_SCHEMA,
});

Supplier.associate = (models) => {
Supplier.belongsToMany(models.category, {
through: models.supplier_category,
onDelete: 'cascade',
})
};

return Supplier;
};

models/category.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Category = sequelize.define('category', {
name: {
type: DataTypes.STRING,
},
}, {
tableName: 'category',
timestamps: false,
schema: process.env.DATABASE_SCHEMA,
});

Category.associate = (models) => {
Category.belongsToMany(models.supplier, {
through: models.supplier_category,
onDelete: 'cascade',
})
};

return Category;
};

models/supplier_category.js

module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const SupplierCategory = sequelize.define('supplier_category', {
'supplierId': {
type: DataTypes.INTEGER,
field: 'supplier_id',
},
'categoryId': {
type: DataTypes.INTEGER,
field: 'category_id',
}
}, {
tableName: 'supplier_category',
timestamps: false,
underscore: true,
schema: process.env.DATABASE_SCHEMA,
});

SupplierCategory.associate = (models) => {
SupplierCategory.belongsTo(models.supplier, {
foreignKey: {
name: 'supplierId',
field: 'supplier_id',
},
as: 'supplier',
});
SupplierCategory.belongsTo(models.category, {
foreignKey: {
name: 'categoryId',
field: 'category_id',
},
as: 'category',
});
};

return SupplierCategory;
};

关于python - 意外错误 : WHERE parameter "undefined" has invalid "undefined" value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com