Stack Exchange
How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]
I have two tables defined in a
 class Patients(tag: Tag) extends Table[(String, String, Int, String)](tag, "Patientss") {
    def PID = column[String]("Patient Id", O.PrimaryKey)

    def Gender = column[String]("Gender")

    def Age = column[Int]("Age")

    def Ethnicity = column[String]("Ethnicity")

    def * = (PID, Gender, Age, Ethnicity)
  }

  val patientsss = TableQuery[Patients]

  class DrugEffect(tag: Tag) extends Table[(String, String, Double)](tag, "DrugEffectss") {

    def DrugID = column[String]("Drug ID", O.PrimaryKey)

    def PatientID = column[String]("Patient_ID")

    def DrugEffectssss = column[Double]("Drug Effect")

    def * = (DrugID, PatientID, DrugEffectssss)

    def Patient = foreignKey("Patient_FK", PatientID, patientsss)(_.PID)
  }

  val d_effects = TableQuery[DrugEffect]

I fill in the tables in this particular object/class as well.
I was wondering how I could call the filled in tables in another object so I can access both DrugEffect and Patients as a class, and then run queries on the table itself?
I hope I'm making myself clear, I don't really have a clue about what I'm doing
What I mean by running queries is something like this:
val q1 = for {
    c <- patientsss if (c.Age === 20 && c.Gender === "F")
    s <- d_effects if (s.DrugEffectssss > 10.0)
  } yield (c.PID, s.DrugID)

but in an object defined in a different file

You need the DB API and the table in the separate class. You can do something like:
import tables.Tables

class SeparateClass extends HasDatabaseConfig[JdbcProfile] {
  val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)

  import driver.api._

  def get(id: Long) = {
    db.run(Tables.DrugEffect.d_effects.filter(_.id === id).result)
  }
}


So just so I understand this, would I then have 3 files: One, where I define all the tables as I defined them here, one where I call the tables to fill them in and one where the resulting filled data is processed? Would I have to do d_effects=TableQuery[tables.DrugEffect]

Yes, that sounds good. Main thing is to have the DB API imported after loading the database configuration, then you can do anything with the DB. The table definition is just that, a definition, it's nothing special - it explains the structure of the table. When you do db.run, it uses this table to work out where to find what. Ditto for when creating the table.

Does the piece of code that you mentioned go in the file where I fill in the data or the file where I define the tables? Currently defined the tables in a separate file but they aren't available in the file where I fill the code. I did import Tables.DrugEffect but DrugEffect wasn't available/could not be resolved

The code I gave in my answer is the minimum you need in every file where you want to do DB operations. The resolution issue could be of two reasons: 1. the import for DrugEffect is incorrect (is it in the Tables package under app?), 2. the DB API wasn't loaded (have you got all the extends, config, etc., that I mentioned, in this file too?)

What do you mean by "Under App"? It's in Tables.scala file, in a subclass called DrugEffect under the Tables class. In that file, I also have val db = Database.forConfig("h2mem1") which I think is the same as your code? I'm trying to call d_effects=TableQuery[Tables.DrugEffect] in the file where I fill the tables

if the path from root project for your "Tables.scala" is "app/tables/Tables.scala", then use the code in my (now edited) answer. :)

Hey, thanks a ton! just got one final question and I think I should be good to go: How would I return the filled in tables? Is there any way to do that?

Not sure I understand your last question, if you mean to return the table contents... You can take this future db.run(q1.result) and block for the results to come out.  E.g., Await.result(db.run(q1.result).map(println), Duration.Inf)

So I have Three Files: 1) Tables defined 2) Tables Filled 3) Tables queried  . In this third file, I need the filled in tables from File 2 to run queries on it.

@S2C The data is in the database, so #3 doesn't need to refer to #2 because it's now all in the database. But maybe I misunderstood you here.
