Skip to content

Default Values not populated into Tables with Default Values. #2

@VoxLight

Description

@VoxLight

I set up a table with this query:

CREATE TABLE "FCMembers" (
	"memberId"	TEXT NOT NULL UNIQUE,
	"name"	TEXT,
	"money"	INTEGER DEFAULT 1000,
	PRIMARY KEY("memberId")
);

Then I created a model with this code:

from sqlitemodel import Model, Database

CONFIG = "path/to/my/db"


class FCMember(Model):
    def __init__(self, memberId: str = "", dbfile=CONFIG):
        Database.DB_FILE = dbfile
        Model.__init__(self, memberId, dbfile=dbfile, foreign_keys=True)
        
        # The DiscordID of this member
        self.memberId = memberId
        # This is implicit from Model
        
        # The DiscordName of this member
        self.name = ""
        
        # The number of fox tokens this member has
        self.money = ""
        
        self.getModel()
        
    @staticmethod
    def tablename():
        return 'FCMembers'
    @staticmethod
    def columns():
        return [
            {
                "name":'memberId',
                "type":'TEXT',
            },
            {
                "name":'name',
                "type":'TEXT',
            },
            {
                "name":'money',
                "type":'INTEGER',
            },
        ]
fc_member = MyModel("1234")
fc_member.createTable()
fc_member.save()

print(fc_member.money) # outputs: ""

And the table had a new entry into it:
memberId name fox_tokens
1

When retrieving the money value, the default value was not applied to the model (which I can understand).

However, the table was given a new row with the correct ID in the ID field, a NULL name field, and a NULL money field. The default value was not populated into the field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions