Error in Room Query to Return Subset of Columns












1















I was playing with Room, where I couldn't find some solution to my queries.



Below is the data.



Table



CREATE TABLE `Employee` (
`id` INTEGER NOT NULL,
`first_name` TEXT,
`last_name` TEXT,
PRIMARY KEY(`id`)
);


Table Data



enter image description here



Entity



@Entity(tableName = "Employee")
public class Employee {

@PrimaryKey
private int id;

@ColumnInfo(name = "first_name")
private String firstName;

@ColumnInfo(name = "last_name")
private String lastName;

..Getters & Setters..
}


Query 1



@Query("Select * from Employee")
List<Employee> getEmployees();


Result
Its successfull



Query 2



@Query("Select first_name, last_name from Employee")
List<Employee> getEmployees();


Result




error: The columns returned by the query does not have the fields [id] in ***.Employee even though they are annotated as non-null or primitive. Columns returned by the query: [first_name, last_name]




If I add id to above Query 2, it works.



Same goes, if we have a Foreign Key in the Table and we try to Query Subset of Columns, it throws Error. The Error goes when we add both Primary Key & Foreign Key Column in the Query.



Question 1
Does that mean we have to always include Primary Key & Foreign Key
(if present) in a Query ?



Question 2
What actually happens under the hood that it throws such error ? Or Am I doing anything wrong ?



Room Version
1.1.1



Also, referred this link but it doesn't solve my issue with Primary Keys.










share|improve this question




















  • 2





    Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

    – 0X0nosugar
    Nov 22 '18 at 19:46











  • Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

    – Himanshu Ahuja
    Nov 22 '18 at 19:56











  • @HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

    – akashPatra
    Nov 23 '18 at 6:03











  • @0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

    – akashPatra
    Nov 23 '18 at 7:05
















1















I was playing with Room, where I couldn't find some solution to my queries.



Below is the data.



Table



CREATE TABLE `Employee` (
`id` INTEGER NOT NULL,
`first_name` TEXT,
`last_name` TEXT,
PRIMARY KEY(`id`)
);


Table Data



enter image description here



Entity



@Entity(tableName = "Employee")
public class Employee {

@PrimaryKey
private int id;

@ColumnInfo(name = "first_name")
private String firstName;

@ColumnInfo(name = "last_name")
private String lastName;

..Getters & Setters..
}


Query 1



@Query("Select * from Employee")
List<Employee> getEmployees();


Result
Its successfull



Query 2



@Query("Select first_name, last_name from Employee")
List<Employee> getEmployees();


Result




error: The columns returned by the query does not have the fields [id] in ***.Employee even though they are annotated as non-null or primitive. Columns returned by the query: [first_name, last_name]




If I add id to above Query 2, it works.



Same goes, if we have a Foreign Key in the Table and we try to Query Subset of Columns, it throws Error. The Error goes when we add both Primary Key & Foreign Key Column in the Query.



Question 1
Does that mean we have to always include Primary Key & Foreign Key
(if present) in a Query ?



Question 2
What actually happens under the hood that it throws such error ? Or Am I doing anything wrong ?



Room Version
1.1.1



Also, referred this link but it doesn't solve my issue with Primary Keys.










share|improve this question




















  • 2





    Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

    – 0X0nosugar
    Nov 22 '18 at 19:46











  • Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

    – Himanshu Ahuja
    Nov 22 '18 at 19:56











  • @HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

    – akashPatra
    Nov 23 '18 at 6:03











  • @0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

    – akashPatra
    Nov 23 '18 at 7:05














1












1








1








I was playing with Room, where I couldn't find some solution to my queries.



Below is the data.



Table



CREATE TABLE `Employee` (
`id` INTEGER NOT NULL,
`first_name` TEXT,
`last_name` TEXT,
PRIMARY KEY(`id`)
);


Table Data



enter image description here



Entity



@Entity(tableName = "Employee")
public class Employee {

@PrimaryKey
private int id;

@ColumnInfo(name = "first_name")
private String firstName;

@ColumnInfo(name = "last_name")
private String lastName;

..Getters & Setters..
}


Query 1



@Query("Select * from Employee")
List<Employee> getEmployees();


Result
Its successfull



Query 2



@Query("Select first_name, last_name from Employee")
List<Employee> getEmployees();


Result




error: The columns returned by the query does not have the fields [id] in ***.Employee even though they are annotated as non-null or primitive. Columns returned by the query: [first_name, last_name]




If I add id to above Query 2, it works.



Same goes, if we have a Foreign Key in the Table and we try to Query Subset of Columns, it throws Error. The Error goes when we add both Primary Key & Foreign Key Column in the Query.



Question 1
Does that mean we have to always include Primary Key & Foreign Key
(if present) in a Query ?



Question 2
What actually happens under the hood that it throws such error ? Or Am I doing anything wrong ?



Room Version
1.1.1



Also, referred this link but it doesn't solve my issue with Primary Keys.










share|improve this question
















I was playing with Room, where I couldn't find some solution to my queries.



Below is the data.



Table



CREATE TABLE `Employee` (
`id` INTEGER NOT NULL,
`first_name` TEXT,
`last_name` TEXT,
PRIMARY KEY(`id`)
);


Table Data



enter image description here



Entity



@Entity(tableName = "Employee")
public class Employee {

@PrimaryKey
private int id;

@ColumnInfo(name = "first_name")
private String firstName;

@ColumnInfo(name = "last_name")
private String lastName;

..Getters & Setters..
}


Query 1



@Query("Select * from Employee")
List<Employee> getEmployees();


Result
Its successfull



Query 2



@Query("Select first_name, last_name from Employee")
List<Employee> getEmployees();


Result




error: The columns returned by the query does not have the fields [id] in ***.Employee even though they are annotated as non-null or primitive. Columns returned by the query: [first_name, last_name]




If I add id to above Query 2, it works.



Same goes, if we have a Foreign Key in the Table and we try to Query Subset of Columns, it throws Error. The Error goes when we add both Primary Key & Foreign Key Column in the Query.



Question 1
Does that mean we have to always include Primary Key & Foreign Key
(if present) in a Query ?



Question 2
What actually happens under the hood that it throws such error ? Or Am I doing anything wrong ?



Room Version
1.1.1



Also, referred this link but it doesn't solve my issue with Primary Keys.







android android-sqlite android-room android-architecture-components






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 6:44







akashPatra

















asked Nov 22 '18 at 19:37









akashPatraakashPatra

5731518




5731518








  • 2





    Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

    – 0X0nosugar
    Nov 22 '18 at 19:46











  • Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

    – Himanshu Ahuja
    Nov 22 '18 at 19:56











  • @HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

    – akashPatra
    Nov 23 '18 at 6:03











  • @0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

    – akashPatra
    Nov 23 '18 at 7:05














  • 2





    Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

    – 0X0nosugar
    Nov 22 '18 at 19:46











  • Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

    – Himanshu Ahuja
    Nov 22 '18 at 19:56











  • @HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

    – akashPatra
    Nov 23 '18 at 6:03











  • @0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

    – akashPatra
    Nov 23 '18 at 7:05








2




2





Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

– 0X0nosugar
Nov 22 '18 at 19:46





Just guessing: "SELECT first_name FROM EMPLOYEE" can only yield a List<String>. If you insist on returning a List<Employee> the runtime will have to fill in some default values. But on the one hand a primary key column can not be filled with null, on the other hand the query result does not have the correct id for a specific first_name value

– 0X0nosugar
Nov 22 '18 at 19:46













Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

– Himanshu Ahuja
Nov 22 '18 at 19:56





Possible duplicate of room error: The columns returned by the query does not have the fields fieldname

– Himanshu Ahuja
Nov 22 '18 at 19:56













@HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

– akashPatra
Nov 23 '18 at 6:03





@HimanshuAhuja: The link I have already referred before posting my question. That link tells about other fields value but mine is related to Primary Keys.

– akashPatra
Nov 23 '18 at 6:03













@0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

– akashPatra
Nov 23 '18 at 7:05





@0X0nosugar: Thanks, I got it know. :) I have edited my post. As, I need List of Employees with first_name and last_name without IDs. Only way I can think of now is, first I have to Query with ID and then create a wrapper to send without IDs.

– akashPatra
Nov 23 '18 at 7:05












2 Answers
2






active

oldest

votes


















1














To select data from multiple fields consider below example.



From the docs




Room allows you to return any Java-based object from your queries as
long as the set of result columns can be mapped into the returned
object. For example, you can create the following plain old Java-based
object (POJO) to fetch the user's first name and last name:




public class NameTuple {
@ColumnInfo(name = "first_name")
public String firstName;

@ColumnInfo(name = "last_name")
public String lastName;
}


Now, you can use this POJO in your query method:



@Dao
public interface MyDao {
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
}





share|improve this answer


























  • Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

    – akashPatra
    Nov 23 '18 at 7:06













  • @akashPatra: check from the docs developer.android.com/training/data-storage/room/…

    – Karan Mer
    Nov 23 '18 at 7:15











  • But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

    – akashPatra
    Nov 23 '18 at 8:39











  • why it will fail? You will keep your employee class as it is.

    – Karan Mer
    Nov 23 '18 at 8:40



















0















I think you should include in
@ColumnInfo(name = "id")
@PrimaryKey
private int id;



because as per the error thrown there is no such column existing as id in your entity by the above query, first the column id is referred as match of the tables column id and then set a primary key via annotation







share|improve this answer
























  • The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

    – akashPatra
    Nov 23 '18 at 6:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437176%2ferror-in-room-query-to-return-subset-of-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To select data from multiple fields consider below example.



From the docs




Room allows you to return any Java-based object from your queries as
long as the set of result columns can be mapped into the returned
object. For example, you can create the following plain old Java-based
object (POJO) to fetch the user's first name and last name:




public class NameTuple {
@ColumnInfo(name = "first_name")
public String firstName;

@ColumnInfo(name = "last_name")
public String lastName;
}


Now, you can use this POJO in your query method:



@Dao
public interface MyDao {
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
}





share|improve this answer


























  • Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

    – akashPatra
    Nov 23 '18 at 7:06













  • @akashPatra: check from the docs developer.android.com/training/data-storage/room/…

    – Karan Mer
    Nov 23 '18 at 7:15











  • But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

    – akashPatra
    Nov 23 '18 at 8:39











  • why it will fail? You will keep your employee class as it is.

    – Karan Mer
    Nov 23 '18 at 8:40
















1














To select data from multiple fields consider below example.



From the docs




Room allows you to return any Java-based object from your queries as
long as the set of result columns can be mapped into the returned
object. For example, you can create the following plain old Java-based
object (POJO) to fetch the user's first name and last name:




public class NameTuple {
@ColumnInfo(name = "first_name")
public String firstName;

@ColumnInfo(name = "last_name")
public String lastName;
}


Now, you can use this POJO in your query method:



@Dao
public interface MyDao {
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
}





share|improve this answer


























  • Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

    – akashPatra
    Nov 23 '18 at 7:06













  • @akashPatra: check from the docs developer.android.com/training/data-storage/room/…

    – Karan Mer
    Nov 23 '18 at 7:15











  • But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

    – akashPatra
    Nov 23 '18 at 8:39











  • why it will fail? You will keep your employee class as it is.

    – Karan Mer
    Nov 23 '18 at 8:40














1












1








1







To select data from multiple fields consider below example.



From the docs




Room allows you to return any Java-based object from your queries as
long as the set of result columns can be mapped into the returned
object. For example, you can create the following plain old Java-based
object (POJO) to fetch the user's first name and last name:




public class NameTuple {
@ColumnInfo(name = "first_name")
public String firstName;

@ColumnInfo(name = "last_name")
public String lastName;
}


Now, you can use this POJO in your query method:



@Dao
public interface MyDao {
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
}





share|improve this answer















To select data from multiple fields consider below example.



From the docs




Room allows you to return any Java-based object from your queries as
long as the set of result columns can be mapped into the returned
object. For example, you can create the following plain old Java-based
object (POJO) to fetch the user's first name and last name:




public class NameTuple {
@ColumnInfo(name = "first_name")
public String firstName;

@ColumnInfo(name = "last_name")
public String lastName;
}


Now, you can use this POJO in your query method:



@Dao
public interface MyDao {
@Query("SELECT first_name, last_name FROM user")
public List<NameTuple> loadFullName();
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 7:14

























answered Nov 23 '18 at 6:26









Karan MerKaran Mer

4,73032864




4,73032864













  • Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

    – akashPatra
    Nov 23 '18 at 7:06













  • @akashPatra: check from the docs developer.android.com/training/data-storage/room/…

    – Karan Mer
    Nov 23 '18 at 7:15











  • But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

    – akashPatra
    Nov 23 '18 at 8:39











  • why it will fail? You will keep your employee class as it is.

    – Karan Mer
    Nov 23 '18 at 8:40



















  • Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

    – akashPatra
    Nov 23 '18 at 7:06













  • @akashPatra: check from the docs developer.android.com/training/data-storage/room/…

    – Karan Mer
    Nov 23 '18 at 7:15











  • But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

    – akashPatra
    Nov 23 '18 at 8:39











  • why it will fail? You will keep your employee class as it is.

    – Karan Mer
    Nov 23 '18 at 8:40

















Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

– akashPatra
Nov 23 '18 at 7:06







Thanks, this looks fine. But, if my Query is "Select first_name, last_name from Employee" like this. I have updated in the post, as I need list of Employees.

– akashPatra
Nov 23 '18 at 7:06















@akashPatra: check from the docs developer.android.com/training/data-storage/room/…

– Karan Mer
Nov 23 '18 at 7:15





@akashPatra: check from the docs developer.android.com/training/data-storage/room/…

– Karan Mer
Nov 23 '18 at 7:15













But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

– akashPatra
Nov 23 '18 at 8:39





But with this my Query 1 will fail. I have to run both Query 1 and Query 2 using same Employee Class.

– akashPatra
Nov 23 '18 at 8:39













why it will fail? You will keep your employee class as it is.

– Karan Mer
Nov 23 '18 at 8:40





why it will fail? You will keep your employee class as it is.

– Karan Mer
Nov 23 '18 at 8:40













0















I think you should include in
@ColumnInfo(name = "id")
@PrimaryKey
private int id;



because as per the error thrown there is no such column existing as id in your entity by the above query, first the column id is referred as match of the tables column id and then set a primary key via annotation







share|improve this answer
























  • The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

    – akashPatra
    Nov 23 '18 at 6:40
















0















I think you should include in
@ColumnInfo(name = "id")
@PrimaryKey
private int id;



because as per the error thrown there is no such column existing as id in your entity by the above query, first the column id is referred as match of the tables column id and then set a primary key via annotation







share|improve this answer
























  • The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

    – akashPatra
    Nov 23 '18 at 6:40














0












0








0








I think you should include in
@ColumnInfo(name = "id")
@PrimaryKey
private int id;



because as per the error thrown there is no such column existing as id in your entity by the above query, first the column id is referred as match of the tables column id and then set a primary key via annotation







share|improve this answer














I think you should include in
@ColumnInfo(name = "id")
@PrimaryKey
private int id;



because as per the error thrown there is no such column existing as id in your entity by the above query, first the column id is referred as match of the tables column id and then set a primary key via annotation








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 19:47









Himanshu AhujaHimanshu Ahuja

6661216




6661216













  • The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

    – akashPatra
    Nov 23 '18 at 6:40



















  • The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

    – akashPatra
    Nov 23 '18 at 6:40

















The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

– akashPatra
Nov 23 '18 at 6:40





The @ColumnInfo(name = "id") doesn't add value to the code. As my variable name is same as column name.

– akashPatra
Nov 23 '18 at 6:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437176%2ferror-in-room-query-to-return-subset-of-columns%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'