光滑的多对一映射

Slick many-to-one mappings

我有3个table,定义为3个case类。 Seller 和 Buyer 都与 Address 具有一对多的关系。有什么方法可以让我在地址 table 中使用一个外键同时指向卖家和买家 table,而不是使用两个外键?我不确定如何编写映射。这是我得到的:

case class Seller(id: Long, name: String)
case class Buyer(id: Long, name: String)
case class Address(id: Long, street: String, city: String, userId: Long)

class SellerTableDef(tag: Tag) extends Table[Seller](tag, "seller") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  override def * = (id, name) <> (Seller.tupled, Seller.unapply)
}

class BuyerTableDef(tag: Tag) extends Table[Seller](tag, "buyer") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  override def * = (id, name) <> (Buyer.tupled, Buyer.unapply)
}

class AddressTableDef(tag: Tag) extends Table[Address](tag, "address") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def street = column[String]("street")
  def city = column[String]("city")
  def userId = column[Long]("user_id")

  //!!!Can I use one foreign key to point to both the Seller and the Buyer table?!!! 
  def user = foreignKey("user_FK", userId, ???)(_.id)
}

非常感谢。

这似乎更像是一个数据库设计问题。

通常的做法是有一个买家 table、一个卖家 table、一个地址 table。然后每个 Buyer/Seller 行都有一个 addressID FK。地址 table 不应该有 Buyer/Seller id,因为地址可能属于多个行

评论后编辑:

在这种情况下,您需要一个 BuyerAddress(与 SellerAddress 相同)table,带有 buyerId 和 addressId,这将保留每个买家的所有地址(buyerId、addressId)

BuyerTable: id, name 卖家表:id、名称 地址表:id、街道、城市、userId BuyerAddressTable: buyerId, addressId SellerAddressTable: sellerId, addressId