为什么列不是通过一对一关系创建的

问题描述 投票:0回答:1

我使用 peewee 作为提供 ORM 功能的包。我创建了通过一对一关系相互关联的模型。以下代码是我的模型。

def __account_model(self) -> Type[Model]:
    db = self.__sql_db

    class AccountModel(Model):
        password = CharField(max_length=255)
        is_successful_authorized = BooleanField(null=True, default=None)

        class Meta:
            database = db

    return AccountModel

def linkedin(self) -> Type[Model]:
    model = self.__account_model

    class Linkedin(model):
        email = ForeignKeyField(self.email, backref='linkedin')
        cookies = TextField(null=True, default=None)

    return Linkedin

def email(self) -> Type[Model]:
    model = self.__account_model

    class Email(model):
        login = CharField(max_length=255)

    return Email

这里我尝试创建 LinkedIn 模型的实例。

password, email_login, email_password, cookies = account.values()
email=controller.email.create(password=email_password,login=email_login)
model.create(password=password, email=email, cookies=cookies)

但是当我运行我的代码时,出现此错误:

Traceback (most recent call last):
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/accounts/build_database.py", line 17, in <module>
    db.build([ModelType.LINKEDIN, ModelType.FACEBOOK, ModelType.GOOGLE,
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/accounts/database.py", line 66, in build
    model.create(password=password, email=email, cookies=cookies)
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 6577, in create
    inst.save(force_insert=True)
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 6787, in save
    pk = self.insert(**field_dict).execute()
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 1966, in inner
    return method(self, database, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2037, in execute
    return self._execute(database)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2842, in _execute
    return super(Insert, self)._execute(database)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2555, in _execute
    cursor = database.execute(self)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 3253, in execute
    sql, params = ctx.sql(query).query()
                  ^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 628, in sql
    return obj.__sql__(self)
           ^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2819, in __sql__
    self._simple_insert(ctx)
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2669, in _simple_insert
    return self._generate_insert((self._insert,), ctx)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 2791, in _generate_insert
    return ctx.sql(CommaNodeList(all_values))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 628, in sql
    return obj.__sql__(self)
           ^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 1832, in __sql__
    ctx.sql(self.nodes[n_nodes - 1])
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 628, in sql
    return obj.__sql__(self)
           ^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 1830, in __sql__
    ctx.sql(self.nodes[i])
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 628, in sql
    return obj.__sql__(self)
           ^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 1430, in __sql__
    return ctx.value(self.value, self.converter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 640, in value
    value = converter(value)
            ^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 5417, in db_value
    return self.rel_field.db_value(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 4744, in db_value
    return value if value is None else self.adapt(value)
                                       ^^^^^^^^^^^^^^^^^
  File "/Users/blnkoff/PycharmProjects/gitcoin-passport-bot/venv/lib/python3.11/site-packages/peewee.py", line 4804, in adapt
    return int(value)
           ^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Email'

但是我没有传递 int 参数,正如您在上面的代码中看到的那样。我尝试检查表的内容,但电子邮件实例已成功创建。

此错误可能与什么有关?

python sql database orm peewee
1个回答
0
投票

我已经知道如何解决这个问题了。这真的很奇怪,但是当我尝试传递 email.id 时,它成功了。

model.create(password=password, email=email.id, cookies=cookies)

太诡异了,因为官方文档中id属性并没有直接传递。

有人明白为什么会这样吗? 当应用程序与后端和 Django 无关时,您使用什么基于 ORM 的包?

© www.soinside.com 2019 - 2024. All rights reserved.