3 Ways to Check if a Table has a Generated Column in SQLite

If you’re not sure if a table has a generated column in SQLite, you can quickly find out with either the .schema command, or by querying the sqlite_schema table.

The .schema Command

Here’s an example of using the .schema command:

.schema Products

Result:

CREATE TABLE Products( 
    Id INTEGER PRIMARY KEY, 
    Name TEXT,
    Qty INT,
    Price INT,
    TotalValue INT GENERATED ALWAYS AS (Qty * Price)
);

The .schema command returns the SQL used to create the table. Therefore, it includes the definition of any generated columns in the table.

In this example, we can see that the TotalValue column is a generated column, because it has GENERATED ALWAYS (followed by the expression that will determine its value).

If you want to include the table data, you can use the .dump command instead of .schema.

The sqlite_schema Table

We can alternatively query the sqlite_schema table:

SELECT sql 
FROM sqlite_schema 
WHERE tbl_name = 'Products';

Result:

CREATE TABLE Products(                              
    Id INTEGER PRIMARY KEY,                         
    Name TEXT,                                      
    Qty INT,                                        
    Price INT,                                      
    TotalValue INT GENERATED ALWAYS AS (Qty * Price)
)      

The sqlite_schema table can also be accessed using sqlite_master (the sqlite_master table was renamed to sqlite_schema in SQLite 3.33.0, but it can still be accessed as sqlite_master for backwards compatibility).