我正在尝试为我使用 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”)
当我从森林管理员导航到供应商并尝试为供应商类别创建一个条目时,我遇到了一个错误消息,
Unexpected error: WHERE parameter "undefined" has invalid "undefined" value
我真的不知道如何解决这个问题。我真的需要一些帮助。
模型/供应商.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;
};
模型/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;
};
模型/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;
};
这是从控制台发送到服务器的请求有效负载:
{
"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 的错误。