Scala 每个文件有一个光滑的表类和外键定义
我正在看Slick 2.0 在中,模式如下所示:Scala 每个文件有一个光滑的表类和外键定义,scala,slick,Scala,Slick,我正在看Slick 2.0 在中,模式如下所示: // Definition of the SUPPLIERS table class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") { def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
// Definition of the SUPPLIERS table
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("SUP_NAME")
def street = column[String]("STREET")
def city = column[String]("CITY")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
// Every table needs a * projection with the same type as the table's type parameter
def * = (id, name, street, city, state, zip)
}
val suppliers = TableQuery[Suppliers] //Definition TableQuery for suppliers
// Definition of the COFFEES table
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {
def name = column[String]("COF_NAME", O.PrimaryKey)
def supID = column[Int]("SUP_ID")
def price = column[Double]("PRICE")
def sales = column[Int]("SALES")
def total = column[Int]("TOTAL")
def * = (name, supID, price, sales, total)
// A reified foreign key relation that can be navigated to create a join
def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id) // Supplier defined above
}
val coffees = TableQuery[Coffees]
这里,TableQuery[Suppliers]
的定义与Coffee
的定义在同一个文件中完成,因此我们可以为foreignKey(supplier
)提供一个TableQuery
)
现在,我想将每个类保存在一个文件中,并在需要时创建TableQuery
我的问题是:
如何在Coffee
类中定义外键,并将Suppliers
类保存在单独的文件中
我是否必须在Scala对象中创建这些TableQuery
,并将其导入Suppliers
类,以便为foreignKey定义提供TableQuery
我希望我足够清楚:/
谢谢您只需参考外键所涉及的
表格查询
:
// SuppliersSchema.scala
object SuppliersSchema {
class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {
/* Omitted for brevity */
}
val suppliers = TableQuery[Suppliers] //Definition TableQuery for suppliers
}
// CoffeesSchema.scala
object CoffeesSchema {
class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {
/* Omitted for brevity */
def supplier = foreignKey("SUP_FK", supID, SuppliersSchema.suppliers)(_.id) // define in another file
}
val coffees = TableQuery[Coffees]
}
另一种方法是在
CoffeesSchema
中创建一个TableQuery
对Suppliers
的引用,并在外键定义键中使用它,无论如何,这种方法还没有经过测试,因为我个人更喜欢第一种。新版本的Play/slick使用了不同的布局,例如-将表/表查询拆分为一个对象似乎不再有效。实际上,我很有信心这仍然有效,您的链接在表的定义方式上没有显示太大的差异。谢谢,我们将重试。在将表/TableQuery拆分为对象时,在导入(dbConfig.\u.和profile.api.\u)方面存在各种问题,但将重新访问。。。