Many to many
See original GitHub issueHi, Is there any support for many to many relations ? If not, How would you implement it ?
I thought about something like that: With doctors having many patients and patients having many doctors, all through an appointment
Doctor.java
@Table
public class Doctor extends BaseModel {
@Column(columnType = Column.PRIMARY_KEY)
public int id;
public List<Appointment> appointments;
private List<Patient> patients;
public Doctor() {
super();
}
public List<Appointment> getAppointments() {
if (appointments == null) {
appointments = Select.all(Appointment.class, Condition.column(Appointment$Table.DOCTORID).is(id));
}
return appointments;
}
public List<Patient> getPatients() {
if (patients == null) {
// patients = { Select.all Patient INNER JOIN Appointments ON Patient.id = patientId }
}
return patients;
}
}
Patient.java
@Table
public class Patient extends BaseModel {
@Column(columnType = Column.PRIMARY_KEY)
public int id;
public List<Appointment> appointments;
private List<Doctor> doctors;
public Patient() {
super();
}
public List<Appointment> getAppointments() {
if (appointments == null) {
appointments = Select.all(Appointment.class, Condition.column(Appointment$Table.PATIENTID).is(id));
}
return appointments;
}
public List<Doctor> getDoctor() {
if (doctors == null) {
// patients = { Select.all Patient INNER JOIN Appointments ON Patient.id = patientId }
}
return doctors;
}
}
Appointment.java
@Table
public class Appointment extends BaseModel {
@Column(columnType = Column.PRIMARY_KEY_AUTO_INCREMENT)
public int id;
public int doctorId;
public int patientId;
@Column(columnType = Column.FOREIGN_KEY,
references = { @ForeignKeyReference(columnType = Appointment.class,
columnName = "appointment", foreignColumnName = "id") })
public Patient patient;
@Column(columnType = Column.FOREIGN_KEY,
references = { @ForeignKeyReference(columnType = Appointment.class,
columnName = "appointment", foreignColumnName = "id") })
public Doctor doctor;
public Appointment() {
super();
}
public Doctor getDoctor() {
return doctor;
}
public Patient getPatient() {
return patient;
}
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:25 (17 by maintainers)
Top Results From Across the Web
Many-to-many (data model) - Wikipedia
In systems analysis, a many-to-many relationship is a type of cardinality that refers to the relationship between two entities, say, A and B, ......
Read more >Many-to-many relationships
A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many ......
Read more >What Is a Many-to-Many Relationship in a Database? An ...
A many-to-many (or M:N) relationship is one of the three database relationships. The other two are: ... By definition, a many-to-many relationship ...
Read more >Video: Create many-to-many relationships - Microsoft Support
A many-to-many relationship exists when one or more items in one table can have a relationship to one or more items in another...
Read more >Many-to-Many Relationship in Database Design - DZone
A many-to-many relationship occurs when multiple records in a table are related to multiple records in another table.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The issue of the logs was coming from the android studio debugger. So it’s not really an issue but rather “latency”.
And for the weird result, I was simply executing a wrong request. So here is the fixed request to retrieve the linked patients.
to answer @agrosner: look at this example here https://github.com/Raizlabs/DBFlow/blob/master/library/src/androidTest/java/com/raizlabs/android/dbflow/test/container/ForeignInteractionModel.java