Skip to content
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

QueryBuilder with model specified does not default to using the model's table #914

Open
1 task
elgow opened this issue Dec 27, 2024 · 0 comments
Open
1 task
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library

Comments

@elgow
Copy link

elgow commented Dec 27, 2024

Describe the feature as you'd like to see it
When a model= is specified in the constructor for a QueryBuilder and no table is explicitly specified by table() then the builder should default to using the model's table.

What do we currently have to do now?
In the current code it is necessary to redundantly specify the table or it defaults to None. This causes exceptions and causes join() etc. to produce broken SQL.

class User(Model):
    __timestamps__ = False
    
QueryBuilder(model=User).table('users').get_table_name()
Out[20]: 'users'

QueryBuilder(model=User).get_table_name()
Traceback (most recent call last):
 ...
 line 228, in get_table_name
    return self._table.name
AttributeError: 'NoneType' object has no attribute 'name'

QueryBuilder(model=User).join('bar').to_sql()
Out[22]: 'SELECT *'

Additional context
When the model is given it is more reasonable to expect that its table will be used. This commit proposes code to fix.

b3f9bca

  • Is this a breaking change?
    No. Explicit table() could still override
@elgow elgow added enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library labels Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature that exists, works as intended but needs to be improved feature request A feature that does not yet exist but will be a good addition to the library
Projects
None yet
Development

No branches or pull requests

1 participant