-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] Need help with table joins #735
Comments
join
@WoolenSweater No one has answered yet so I'll try even though I can't try it. Try passing the client as fk to the class Client(Table):
report_id = ForeignKey(references=Report)
type = SmallInt()
# columns
class Message(Table):
id = UUID(primary_key=True)
report_id = ForeignKey(references=Report)
status = ForeignKey(references=Status, target_column=Status.code)
client = ForeignKey(references=Client)
# columns and then use like this. result = await Message.select(
Message.id,
Message.report_id,
Message.report_id.form,
Message.status.name,
Message.client.report_id,
Message.client.type
).where(
Message.status.code != 0
) If that doesn't work, you can always use raw query |
@sinisaos Thank you for your response.I thought about this way, but it doesn't work. LEFT JOIN "client" "message$client" ON ("message"."client" = "message$client"."id")
I would not like to resort to raw request. Perhaps I'm missing something else. I looked in the documentation for methods for joining tables, but they are not there. It seems to me that it would be great if such methods were available if there is no way to access the table through a foreign key. Something like this. result = await Message.select(
Message.id,
Message.status.name,
Client.report_id,
Client.type,
).left_join(
Client, on=Client.report_id == Message.report_id
).where(
Message.status.code != 0
) or result = await Message.select(
Message.id,
Message.status.name,
Client.report_id,
Client.type,
).left_join(
Client
).on(
Client.report_id == Message.report_id
).where(
Message.status.code != 0
) |
@WoolenSweater Piccolo doesn't expose join syntax (similarly to Django) and there is no methods like class Status(Table):
code = SmallInt()
name = Varchar()
class Client(Table):
type = SmallInt()
# columns
class Report(Table):
id = UUID(primary_key=True)
form = Varchar()
client = ForeignKey(references=Client)
# columns
class Message(Table):
id = UUID(primary_key=True)
report_id = ForeignKey(references=Report)
status = ForeignKey(references=Status)
# columns and then you can use your previous query result = await Message.select(
Message.id,
Message.report_id,
Message.report_id.form,
Message.status.name,
Message.report_id.client.type,
).where(Message.status.code != 0) If that doesn't work well, I'm out of ideas other than a raw sql query. |
Passing the client as fk to the
@dantownsend Where can I follow these updates? |
This is it - we're going to add a In the meantime, something like this is probably what you need: clients = await Client.select(Client.type. Client.report_id)
clients_map = {i['report_id']: i for i in clients}
messages = await Message.select(
Message.status.name,
Message.status.code,
Message.report_id,
Message.report_id.form
)
combined = [{**i, **clients_map[i['report_id']]} for i in messages] |
@WoolenSweater Sorry, but this is not true (I tried that and there is no migration error or any other error). That works if you change schema (add client fk to |
Yes, by changing the schema, you can make any method work. I should have said right away that I can't do it, sorry. Perhaps I will focus on the method that @dantownsend advised. |
I have following tables.
I want to make this query.
This variant returns a rather obvious error.
How can I join a table with client details?
The text was updated successfully, but these errors were encountered: