Repository navigation
Alias self-referencing relations in where_has/has #321
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
Changes from all commits
12b609f
876e241
d502645
9834b9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,9 +98,11 @@ def __init__( | |
| lock=False, | ||
| having=(), | ||
| connection_details=None, | ||
| alias=None, | ||
| ): | ||
| self._columns = columns | ||
| self.table = table | ||
| self.alias = alias | ||
| self.database = database | ||
| self._wheres = wheres | ||
| self._limit = limit | ||
|
|
@@ -161,7 +163,7 @@ def _compile_select(self, qmark=False): | |
| .format( | ||
| columns=self.process_columns(separator=", ", qmark=qmark), | ||
| keyword="DISTINCT" if self._distinct else "", | ||
| table=self.process_table(self.table), | ||
| table=self.process_from_table(), | ||
| joins=self.process_joins(qmark=qmark), | ||
| wheres=self.process_wheres(qmark=qmark), | ||
| limit=self.process_limit(), | ||
|
|
@@ -560,6 +562,11 @@ def process_table(self, table): | |
| for t in table.name.split(".") | ||
| ) | ||
|
|
||
| def process_from_table(self): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. Only SELECT honours the alias, and insert/update/delete still use |
||
| if not self.alias: | ||
| return self.process_table(self.table) | ||
| return f"{self.process_table(self.table)} AS {self.process_table(self.alias)}" | ||
|
|
||
| def process_limit(self): | ||
| """Compiles the limit expression. | ||
|
|
||
|
|
@@ -960,7 +967,7 @@ def process_column(self, column, separator=""): | |
| table = None | ||
| if column and "." in column: | ||
| table, column = column.split(".") | ||
| return self.column_string().format(column=column, separator=separator, table=table or self.table) | ||
| return self.column_string().format(column=column, separator=separator, table=table or self.alias or self.table) | ||
|
|
||
| def _table_column_string(self, column, alias=None, separator=""): | ||
| """Compiles a column into the column syntax. | ||
|
|
@@ -982,15 +989,15 @@ def _table_column_string(self, column, alias=None, separator=""): | |
| return self.column_strings["select_all"].format( | ||
| column=column, | ||
| separator=separator, | ||
| table=self.process_table(table or self.table), | ||
| table=self.process_table(table or self.alias or self.table), | ||
| ) | ||
|
|
||
| alias_string = " " + self.subquery_alias_string().format(alias=alias) if alias else "" | ||
| return self.column_strings[self._action].format( | ||
| column=column, | ||
| separator=separator, | ||
| alias=alias_string, | ||
| table=self.process_table(table or self.table), | ||
| table=self.process_table(table or self.alias or self.table), | ||
| ) | ||
|
|
||
| def _compile_value(self, value, separator=""): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,15 @@ def model_reference(self) -> str | type: | |
| def get_builder(self): | ||
| return self.fn()().get_builder() | ||
|
|
||
| def exists_alias(self, query, parent): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocker (process): this core relationship change needs an ADR in |
||
| if query.get_table_name() == parent._table: | ||
| return f"{parent.get_table_name()}_self" | ||
| return None | ||
|
|
||
| def get_exists_builder(self, parent): | ||
| query = self.get_builder() | ||
| return query.table(query.get_table_name(), self.exists_alias(query, parent)) | ||
|
|
||
| @overload | ||
| def __get__(self, instance: None, owner: Any) -> Self: ... | ||
| @overload | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking:
get_table_name()now returns the alias. That works for every relationship caller I traced. Note that a callback qualifying the real table name (e.g.q.where("members.name", ...)) now binds to the outer row instead of the inner one. Please document this and pin it with a test.