我正在尝试为我使用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”)
当我从林管理员导航到供应商并尝试为vendor_categories创建条目时,我遇到了一条错误消息,
Unexpected error: WHERE parameter "undefined" has invalid "undefined" value
我真的不确定如何解决此问题。我真的需要一些帮助。
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;
};
这是从控制台发送到服务器的请求有效负载:
{
"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。
答案 0 :(得分:1)
supplier
和category
之间的关系是通过联接表SupplierCategory
定义的。
Forest Admin已插入SQL数据库,并使用Express / Sequelize为您提供admin 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;
};