Creating a User Table with Random Fields

  • Share this:

Code introduction


This function creates a new table named User with fields like username, email, age, and is_active. It ensures that the table exists in the specified database.


Technology Stack : Peewee

Code Type : Database operation

Code Difficulty : Intermediate


                
                    
def create_random_user_table(database):
    """
    This function creates a new user table in the given database with random fields.
    """
    # Define the User model with Peewee
    class User(peewee.Model):
        username = peewee.CharField()
        email = peewee.CharField()
        age = peewee.IntegerField()
        is_active = peewee.BooleanField()

        class Meta:
            database = database

    # Create the table if it does not exist
    database.connect()
    database.create_tables([User])
    database.close()

# Peewee library is used here                
              
Tags: