3

Is it possible to write a statement in sqlite.swift that will generate the equivalent sql:

SELECT foods.name, food_types.name FROM foods, food_types WHERE foods.type_id=food_types.id LIMIT 10;

I can't figure out how to query from multiple Table objects at once. Thanks!

2 Answers 2

7

Your original query passes two tables to the FROM clause, creating an implicit join. SQLite.swift's query builder language currently only supports explicit joins.

Check out the documentation under Joining Other Tables for more information on joining tables.

In your case:

let foods = Table("foods")
let food_types = Table("food_types")
let name = Expression<String>("name")
let id = Expression<Int64>("id")
let type_id = Expression<Int64>("type_id")

let query = foods
    .select(foods[name], food_types[name])
    .join(food_types, on: foods[type_id] == food_types[id])
    .limit(10)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Stephen, I've tried that and it works, I'm running into trouble when I try and join more than two tables, I have multiple tables all with the same foreign key and I'm trying to join on it but I don't get the expected result, I get way more rows than expected. I can do two at a time, and then build my data structure from there, but I was looking for something more elegant. I really appreciate your work and your well designed Sqlite.swift. Cheers.
Without an example of what isn't working, I unfortunately don't have the ability to help further :( .joins can be chained, so you should be able to continue to build up more complex joins.
Thanks for for the follow up, I think I figured it out. The foreign key is a column for all the tables I'm trying to join, but there are foreign key members that are not common among all the tables so I believe sql then generates a a cross join vs an inner join... That leads to all the extra rows in the query. I confirmed this by using the sql that sqlite.swift generates on the db directly. Thanks again and I'll close out the question
@stephencelis. I'm not sure anyone in the universe can answer this one other than you: stackoverflow.com/questions/41874682/… !! I'm stumped.
SO well written... thank you! I'd been looking for a good example on how to do this for months and had always just hacked my way around it and now its very clear.
0

I figured it out. The foreign key is a column for all the tables I'm trying to join, but there are foreign key members that are not common among all the tables so I believe sql then generates a a cross join vs an inner join... That leads to all the extra rows in the query. I confirmed this by using the sql that sqlite.swift generates on the db directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.