ValueError: 'missing' must not be set for required fields.
Opened this issue · 3 comments
I have recently updated to v3.0.0 and those errors are now popping up while trying to instantiate a schema:
Traceback (most recent call last):
File "C:\Users\Redacted\lab-assistant\lab-assistant-env\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Redacted\lab-assistant\cogs\botmonitor.py", line 45, in on_member_update
guild_data = await self.bot.get_guild_data(guild)
File "C:\Users\Redacted\lab-assistant\internal\botclass.py", line 62, in get_guild_data
from database.Guild import Guild
File "C:\Users\Redacted\lab-assistant\database\Guild.py", line 6, in <module>
from database.Team import Team
File "C:\Users\Redacted\lab-assistant\database\Team.py", line 7, in <module>
class Team(Document):
File "C:\Users\Redacted\lab-assistant\database\Team.py", line 12, in Team
role_id = IntField(required=True, default=0)
File "C:\Users\Redacted\lab-assistant\lab-assistant-env\lib\site-packages\umongo\abstract.py", line 133, in __init__
super().__init__(*args, **kwargs)
File "C:\Users\Redacted\lab-assistant\lab-assistant-env\lib\site-packages\marshmallow\fields.py", line 940, in __init__
super().__init__(**kwargs)
File "C:\Users\Redacted\lab-assistant\lab-assistant-env\lib\site-packages\marshmallow\fields.py", line 889, in __init__
super().__init__(**kwargs)
File "C:\Users\Redacted\lab-assistant\lab-assistant-env\lib\site-packages\marshmallow\fields.py", line 188, in __init__
raise ValueError("'missing' must not be set for required fields.")
ValueError: 'missing' must not be set for required fields.
However, I am never setting 'missing' anywhere. Here's a document registration example:
from umongo import Document, validate
from umongo.fields import *
from internal.database_init import instance
@instance.register
class Team(Document):
"""A team table"""
guild = IntField(required=True)
name = StrField(required=True, validate=validate.Length(min=1, max=32))
role_id = IntField(required=True, default=0)
description = StringField(required=True, validate=validate.Length(min=1, max=1000))
joinable = BoolField(required=True, default=False)
leader_id = IntField(required=True, default=0)
member_perms = ListField(StrField, required=True, default=[])
codes = ListField(StrField, required=True, default=[])
Could anyone tell me why this is happening? I use defaults to not create duplicated code on my insertions.
default
in umongo translates to missing
in marshmallow.
missing
+ required
is forbidden in marshmallow.
Admittedly, the error message is not that helpful. Perhaps we could catch that earlier.
What's the point of having a default
on a required
field?
What's the point of having a
default
on arequired
field?
It's so when I create a document I need to initialize some defaults that are always the same and also required for the doc to work without many checks, and since I have multiple sources of insertions, it's better to have those static values set in there. If there's another recommended way to do this, I'm all ears.
I don't get it.
required
means that if the value is missing, an exception is raised.default
is the value used if the value is missing
You can't have both. If you set a default, the value may be missing in the input, it will be replaced by the default value. No need to set required
.