0

I've seen this issue room-error-the-columns-returned-by-the-query-does-not-have-the-fields-fieldname but I cant apply it to my own error.

This is my Movie class

    @Entity
    public class Movie {

        @PrimaryKey
        @NonNull
        public String id;

        @ColumnInfo
        public String name;

        @ColumnInfo
        public String title;
    }

This is my UpcomingMovies class

@Entity(
        foreignKeys = {
        @ForeignKey(
                entity = Movie.class,
                parentColumns = "id",
                childColumns = "movieId")
        },
        indices = @Index("movieId")
)
@TypeConverters(MovieConverter.class)
public class UpcomingMovies {

    @PrimaryKey(autoGenerate = true)
    public int id;

    @ColumnInfo
    public int movieId;

    @ColumnInfo
    public Movie movie;
}

The query I'm trying to run, which I want to get a List of movies

@Query("Select movie from UpcomingMovies")
LiveData<List<Movie>> loadUpcomingMovies();

And the specific error I'm getting

error: The columns returned by the query does not have the fields [id] in *.Model.POJOs.Movie even though they are annotated as non-null or primitive. Columns returned by the query: [movie]

What've I missed here ?

Edit: Added MovieConverter

public class MovieConverter {

    @TypeConverter
    public String movieToString(Movie movie) {
        return new Gson().toJson(movie);
    }

    @TypeConverter
    public Movie stringToMovie(String src) {
        return new Gson().fromJson(src, Movie.class);
    }
}
Greggz
  • 1,873
  • 1
  • 12
  • 31

1 Answers1

1

Try this query:

@Query("Select * from UpcomingMovies")
LiveData<List<UpcomingMovies>> loadUpcomingMovies();

And have a GETTER method in your UpcomingMovies class:

public Movie getMovie() {
    return this.movie;
}

You can also get list of movies with this query:

@Query("Select * from Movie")
LiveData<List<Movie>> loadUpcomingMovies();

Good luck :)

Tomas Jablonskis
  • 4,246
  • 4
  • 22
  • 40
  • Thanks bro, the error went away, can you provide some explanation ? Thanks for your time :) – Greggz Aug 27 '18 at 16:07
  • @Greggz The thing is, that you were trying to get a list of **Movie** objects, while **UpcomingMovies** only has one instance of it. – Tomas Jablonskis Aug 27 '18 at 16:10
  • But can't I just return the list of movies from the Upcoming table ? – Greggz Aug 27 '18 at 16:14
  • Hum.. I though every row would have a Movie – Greggz Aug 27 '18 at 16:15
  • No. If I were you I would better of query Movie table with specific parameter in SQL query, to get specific list of Movies. – Tomas Jablonskis Aug 27 '18 at 16:15
  • Well, you are correct, every row of UpcomingMovies has a Movie instance. But its the same as if you were getting just a simple Movie list from Movie table. – Tomas Jablonskis Aug 27 '18 at 16:16
  • @Greggz Both queries that I provided above will return the same List of Movies, just that it will be wraped in UpcomingMovie class if using the first one. – Tomas Jablonskis Aug 27 '18 at 16:17
  • @Greggz If you want to have List while querying UpcomingMovies table, you should look into MANY:MANY table relasionships. You can look up the implementation [here](https://android.jlelse.eu/android-architecture-components-room-relationships-bf473510c14a) – Tomas Jablonskis Aug 27 '18 at 16:20
  • I see that, so if `Select * from UpcomingMovies` will return movies with their `movieId` , why `Select movie from UpcomingMovies` doesn't return every movie instance present in the table ? – Greggz Aug 27 '18 at 16:21
  • Thanks! That was just what I was about to ask you, some documentation, cause clearly my knowledge is lacking.. Thank you so much! Cheers – Greggz Aug 27 '18 at 16:21