1

我正在尝试为我使用 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 的错误。

4

1 回答 1

1

supplier和之间的关系category是用连接表定义的SupplierCategory

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

因此,在您的情况下,您必须使用belongsToMany关联。请参阅此处获取链接。

您的模型应如下所示:

模型/供应商.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;
};

模型/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;
};

模型/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;
};
于 2019-11-21T07:27:29.973 回答