tortoise / tortoise/tortoise-orm

Error adding data using create

Open
#767 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

register_tortoise uses the config configuration to report an error when adding data

models:
`
class Group(Model):
"""
"""
group_id = fields.IntField(pk=True)
group_name = fields.CharField(max_length=255)
establish_user_id = fields.IntField()
state = fields.IntField()
create_date = fields.DatetimeField(auto_now_add=True)

class Meta:
    table = "mod_group"

`

DB config:
register_tortoise( app, config={ 'connections': { # Dict format for connection 'default': { 'engine': 'tortoise.backends.mysql', 'credentials': { 'host': config.DB_HOST, 'port': config.DB_PORT, 'user': config.DB_USER, 'password': config.DB_PASS, 'database': config.DB_NAME, } } }, 'apps': { 'models': { 'models': ['__main__'], # If no default_connection specified, defaults to 'default' 'default_connection': 'default', } } }, modules={"models": ["models.models"]}, generate_schemas=config.GENERATE_SCHEMA )

Code:
`
try:
group = await Group.create(
group_name=filter_tags(request.form.get("group_name", 0)),
establish_user_id=user_id,
state=1
)
result = {"code": 1, "message": "success"}

except IntegrityError as ie:
print(ie)
result = {"code": 0, "message": "data existed!"}

except Exception as ex:
print(ex)
result = {"code": 0, "message": f"{ex}"}

return json(result)
`

Error:
`
Traceback (most recent call last):
File "F:\work\python\test\api\venv\lib\site-packages\sanic\app.py", line 724, in handle_request
response = await response
File "F:\work\python\test\api\api_sanic\views\my_group\my_group.py", line 51, in group
state=1
File "F:\work\python\test\api\venv\lib\site-packages\tortoise\models.py", line 1095, in create
await instance.save(using_db=db, force_create=True)
File "F:\work\python\test\api\venv\lib\site-packages\tortoise\models.py", line 898, in save
executor = db.executor_class(model=self.class, db=db)
File "F:\work\python\test\api\venv\lib\site-packages\tortoise\backends\base\executor.py", line 92, in init
self.model._meta.basequery.where(
AttributeError: 'Query' object has no attribute 'where'

`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Group.create call with the register_tortoise configuration, then inspect tortoise/models.py and tortoise/backends/base/executor.py at the traceback locations. Confirm why the base query lacks the reported method and identify a regression test covering creation with this configuration; done means the insert completes without the AttributeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.