Randomly Select Column from SQLAlchemy Table

  • Share this:

Code introduction


This function randomly selects a column from the specified SQLAlchemy table object.


Technology Stack : SQLAlchemy, SQLAlchemy-Utils

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_random_column_from_table(session, table_name):
    from sqlalchemy_utils import random_column
    import random

    table = session.query(table_name).first()
    if table:
        column_name = random_column(table)
        return column_name
    else:
        raise ValueError("Table not found")