ormar
ormar copied to clipboard
Joins result in an error with tables that have a period in their name
Describe the bug
https://github.com/collerek/ormar/blob/a27d7673a5354429bef4158297d76a58522c1579/ormar/queryset/join.py#L112
Because SqlJoin._on_clause
expects a from_clause
string in the format of "table_name.column_name", if your table name has a period in it, the string will be chunked into more than two parts, causing an error when it cannot set the two variables.
To Reproduce Steps to reproduce the behavior:
- Create a model with a foreign key and a tablename with a period in it
- Attempt to filter data over the foreign key relation, which should result in a join and then in an error
ValueError: too many values to unpack (expected 2)
Expected behavior Ormar handles the period without issue.
Screenshots If applicable, add screenshots to help explain your problem.
Versions (please complete the following information):
- Database backend used: MySQL and SQLite
- Python version: 3.10
-
ormar
version: 0.11.2 -
pydantic
version: 1.9.1
Solution
One solution I would propose since users generally don't use periods in their columns is to just reserve the last chunk for the column name and do something like this.
parts = from_clause.split(".")
table = ".".join(parts[:-1])
column = parts[-1]
However, a more robust solution would be to just keep table name and column name separate strings until they are run through quotter()
.
Let me know if there is a preferred method to solve this and I'll open a pull request.