Skip to content

Commit

Permalink
wip, has issues
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Ramsay <seapagan@gmail.com>
  • Loading branch information
seapagan committed Sep 29, 2024
1 parent 627dea1 commit 57a6098
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class UserModel(BaseDBModel):
class Meta:
"""Override the default options for the UserModel."""

create_pk: bool = False # Disable auto-increment ID
primary_key: str = "slug" # Use 'slug' as the primary key
create_pk: bool = True # Disable auto-increment ID
# primary_key: str = "slug" # Use 'slug' as the PK # noqa: ERA001
table_name: str = "users" # Explicitly define the table name


Expand All @@ -41,7 +41,7 @@ def main() -> None:
level=logging.DEBUG, format="%(levelname)-8s%(message)s"
)

db = SqliterDB(memory=True, auto_commit=True, debug=True)
db = SqliterDB("poop.db", memory=False, auto_commit=True, debug=True)
with db:
db.create_table(UserModel) # Create the users table
user1 = UserModel(
Expand Down
9 changes: 9 additions & 0 deletions sqliter/query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def fields(self, fields: Optional[list[str]] = None) -> QueryBuilder:
"""
if fields:
self._fields = fields
# Ensure the primary key is included and placed first if create_pk
# is True
if self.model_class.should_create_pk():
primary_key = self.model_class.get_primary_key()
if primary_key in self._fields:
self._fields.remove(
primary_key
) # Remove if it's already there
self._fields.insert(0, primary_key) # Add it at the beginning
self._validate_fields()
return self

Expand Down
8 changes: 8 additions & 0 deletions sqliter/sqliter.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,14 @@ def select(
Returns:
A QueryBuilder instance for further query construction.
"""
# Ensure the primary key is included and placed first
if model_class.should_create_pk():
primary_key = model_class.get_primary_key()
if fields:
if primary_key in fields:
fields.remove(primary_key)
fields.insert(0, primary_key)

query_builder = QueryBuilder(self, model_class, fields)

# If exclude is provided, apply the exclude method
Expand Down

0 comments on commit 57a6098

Please sign in to comment.