Reputation: 153
On the Getting started page in DBFlow git: https://github.com/Raizlabs/DBFlow/blob/master/usage/GettingStarted.md
They explain how to create a one to many relation, but one thing is not clear and Android Studio complains about these parts:
.where(Ant_Table.queenForeignKeyContainer_id.eq(id))
Where does Ant_Table comes from? Does it need any declaration?
Same goes for queenForeignKeyContainer_id.
Upvotes: 2
Views: 3002
Reputation: 714
@Table(database = AppDatabase.class)
public class Department extends BaseModel {
@Column
@PrimaryKey(autoincrement = true)
private int d_id;
@Column
@Unique
public String department_name;
public String getDepartment_name() {
return department_name;
}
public void setDepartment_name(String department_name) {
this.department_name = department_name;
}
public int getD_id() {
return d_id;
}
public void setD_id(int d_id) {
this.d_id = d_id;
}
}
This Is my Department table
@Table(database = AppDatabase.class)
public class CompleteProfile extends BaseModel {
@Column
@PrimaryKey(autoincrement = true)
int id;
@Column
@Unique
private String stud_username;
@Column
@Unique
private String stud_password;
@Column
private int d_id;
public String getStud_username() {
return stud_username;
}
public void setStud_username(String stud_username) {
this.stud_username = stud_username;
}
public String getStud_password() {
return stud_password;
}
public void setStud_password(String stud_password) {
this.stud_password = stud_password;
}
List<Department> departments;
@OneToMany(methods = OneToMany.Method.ALL, variableName = "departments")
public List<Department> dbFlowOneTwoManyUtilMethod() {
if (departments == null) {
departments = SQLite.select()
.from(Department.class)
.where(Department_Table.d_id.eq(d_id))
.queryList();
}
return departments;
}
public void setD_id(int d_id) {
this.d_id = d_id;
}
public int getD_id() {
return d_id;
}
}
Complete Profile table(model class) references department table,d_id is foreign Key
Cursor cursor = new Select(Department_Table.department_name, CompleteProfile_Table.stud_username, CompleteProfile_Table.stud_password)
.from(Department.class)
.join(CompleteProfile.class, Join.JoinType.INNER)
.on(Department_Table.d_id.withTable()
.eq(CompleteProfile_Table.d_id.withTable())).query();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Log.e("USERNAME=", cursor.getString(1));
Log.e("PASSWORD=", cursor.getString(2));
Log.e("DEPARTMENT NAME=", cursor.getString(0));
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
This is query to fetch data from both table
Upvotes: 0
Reputation: 1210
- Where does Ant_Table comes from?
The Ant_Table
doesn't need to declare.The class is just generated after you compile your project(indeed DBFlow use java annotation to generate these source code).
So you could comment these wrong code first, then compile these model class. Then DBFlow generate some code for you, now you can uncomment these code.
queenForeignKeyContainer_id
is also generated by DBFlow.Upvotes: 4
Reputation: 1013
If the Ant_Table does not compile, ensure that you have set up the Application class. Ant_Table as a final class along with the foreign key member that represents what you added to your schema to correlate the two tables. It is really confusing not to mention that in the docs.
Upvotes: 0
Reputation: 5791
I had the same issue. Finally solved it by modeling the where condition as string, like this:
String fieldName = MyDBEntry$Table.NOTIFICATIONID;
long value = parent.getId();
final String whereClause = String.format("%s = %d", fieldName, value);
TransactionManager.getInstance().addTransaction(
new SelectListTransaction<>(
new Select().from(MyDBEntry.class)
.where(whereClause),
new TransactionListenerAdapter<List<MyDBEntry>>() {
@Override
public void onResultReceived(List<MyDBEntry> dbList) {
...
}
})
);
Just replace fieldName and value with your desired values
Upvotes: 0
Reputation: 153
Ok, so i was not seeing the gradle build error that was showing when i was trying to make project.
But even with the message, it was still not clear. I needed to do the following: File>Invalidate caches/restart
After the restart, when i tried to make the project again, the error was obvious. It seems that DBFlow doesn't have enough information about which field can/should be visible or (on my case, private), so i had a private primary key with getters and setters, which was causing problems with the foreign key.
Just removed the private and the problem was solved.
Upvotes: -1