tortoise / tortoise/tortoise-orm

Function as insert value

Open
#1,680 1 comment 0 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

im trying to convert this code to tortoise

table = Table('linkedin_logging')

q = MySQLQuery.into(table).columns(
    table.date, table.withdraw, table.connections, table.whatsapp, table.like
).insert(
    CurDate(), 0, 1, 0, 0
).on_duplicate_key_update(
    table.connections, table.connections + 1
)

date is a unique

this is the table sql


CREATE TABLE `linkedin_logging` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `withdraw` bigint(20) NOT NULL DEFAULT 0,
  `connections` bigint(20) NOT NULL DEFAULT 0,
  `whatsapp` bigint(20) NOT NULL DEFAULT 0,
  `like` bigint(20) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `linkedin_logging_date_unique` (`date`)
) ENGINE=InnoDB AUTO_INCREMENT=478747 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

whenever i run it i get

Traceback with variables (most recent call last):
  File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 1140, in create
    instance = cls(**kwargs)
      cls = <class 'models.models.LinkedinLogging'>
      using_db = None
      kwargs = {'withdraw': 1, 'date': <pypika.functions.CurDate object at 0x79541ad1c790>}
  File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 660, in __init__
    for key in meta.fields.difference(self._set_kwargs(kwargs)):
      self = <exception while printing> Traceback (most recent call last):
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 222, in _to_cropped_str
            raw = print_(obj)
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 767, in __repr__
            if self.pk:
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 784, in _get_pk_val
            return getattr(self, self._meta.pk_attr)
        AttributeError: 'LinkedinLogging' object has no attribute 'id'
        
      kwargs = {'withdraw': 1, 'date': <pypika.functions.CurDate object at 0x79541ad1c790>}
      meta = <tortoise.models.MetaInfo object at 0x79541af5e980>
  File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 696, in _set_kwargs
    setattr(self, key, field_object.to_python_value(value))
      self = <exception while printing> Traceback (most recent call last):
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/traceback_with_variables/core.py", line 222, in _to_cropped_str
            raw = print_(obj)
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 767, in __repr__
            if self.pk:
          File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/models.py", line 784, in _get_pk_val
            return getattr(self, self._meta.pk_attr)
        AttributeError: 'LinkedinLogging' object has no attribute 'id'
        
      kwargs = {'withdraw': 1, 'date': <pypika.functions.CurDate object at 0x79541ad1c790>}
      meta = <tortoise.models.MetaInfo object at 0x79541af5e980>
      passed_fields = {'withdraw', 'date'}
      key = 'date'
      value = <pypika.functions.CurDate object at 0x79541ad1c790>
      field_object = <tortoise.fields.data.DateField object at 0x79541acc33a0>
  File "/home/dell/Desktop/bot_sqlV4/tortoise/venv/lib/python3.10/site-packages/tortoise/fields/data.py", line 408, in to_python_value
    value = parse_datetime(value).date()
      self = <tortoise.fields.data.DateField object at 0x79541acc33a0>
      value = <pypika.functions.CurDate object at 0x79541ad1c790>
builtins.TypeError: argument must be str

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

Start in tortoise/models.py at the create path shown in the traceback, then inspect tortoise/fields/data.py where DateField.to_python_value receives the CurDate expression. Reproduce the provided MySQL model and insert call, and verify that a function can be used as an insert value without the date conversion error.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.