Column
Represents a database column with various constraints and properties.
Parameters:
-
column_name
(str
) –The name of the column.
-
data_type
(str
) –The data-type of the column (e.g., integer,varchar,date).
-
primary_key
(Optional[bool]
, default:False
) –Whether the column is a primary key or not . Defaults to False.
-
foreign_key
(Optional[bool]
, default:False
) –Whether the column is a foreign key or not. Defaults to False.
-
unique
(Optional[bool]
, default:False
) –Whether the column has a unique constraint. Defaults to False.
-
null
(Optional[bool]
, default:True
) –Whether the column allows NULL values. Defaults to True.
-
max_length
(Optional[int]
, default:None
) –The maximum length of the column (for data types like varchar,char).
-
precision
(Optional[int]
, default:None
) –The precision for numeric types (total number of digits). Defaults to None.
-
scale
(Optional[int]
, default:None
) –The scale for numeric types (number of digits to the right of the decimal).
-
referenced_column
(Optional[str]
, default:None
) –The name of the referenced column if current column is a foreign key.
-
referenced_table
(Optional[str]
, default:None
) –The name of the referenced table if current column is a foreign key.
-
on_delete
(Optional[str]
, default:None
) –The ON DELETE strategy for foreign key constraints.Defaults to CASCADE.
-
foreign_key_constraint_name
(Optional[str]
, default:None
) –The name of the foreign key constraint. Defaults to None.
Usage
from polly.auth import Polly from polly.atlas import Atlas, Table, Column
Polly.auth("
column = Column(column_name="user_id", data_type="integer", primary_key=True, unique=True, null=False)
Constraints
- Primary Key: Combines not null + unique , ensuring each row has a unique identifier.
- Foreign Key: Establishes foriegn key relationships between tables.
- Unique: Ensures all values in a column are distinct.
- Null: Ensures if a column can be nullable or not.
Supported Data Types
Numeric Data Types
smallint
(2 bytes): Range -32,768 to 32,767integer
(4 bytes): Range -2,147,483,648 to 2,147,483,647bigint
(8 bytes): Range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807decimal(p,s) / numeric(p,s)
: Variable storage, exact precision up to 38 digitsreal
(4 bytes): Single-precision floating-point numbersdouble precision
(8 bytes): Double-precision floating-point numbersserial / bigserial
: Auto-incrementing integer typesmoney
(8 bytes): Fixed-point for currency values
Note: bigserial
can only be used as a primary key column.
`precision` and `scale` settings are applicable only to decimal and numeric types.
Character Data Types
char(max_length) / character(max_length)
: Fixed-length string (padded with spaces)varchar(max_length) / character varying(max_length)
: Variable-length stringtext
: Unlimited-length string
Note: max_length
is required for char and varchar. text does not require a length constraint.
Date & Time Data Types
date
: Stores calendar dates (YYYY-MM-DD
)time
: Stores time of day (HH:MM:SS
)timestamp
/timestamp without time zone
: Stores date and time without timezone(2025-04-03 14:30:00
)timestamptz
/timestamp with time zone
: Stores date and time with timezone(2025-04-03 14:30:00+05:30
)interval
: Represents a duration (e.g.,"2 hours 30 minutes"
)
Boolean & JSON Data Types
boolean
: Storestrue
,false
valuesjson
: Stores structured JSON data
Examples
Using Primary Key
This is how you declare a column as Primary Key
Using Foreign Key
A foreign key enforces referential integrity by linking a column to another table.
column = Column(column_name="patient_id",data_type="integer",foreign_key=True,referenced_table="patients",referenced_column="id"on_delete="CASCADE")
Unique Constraint
Ensures that column contains unique values.
column = Column(column_name="prescription", data_type="varchar", max_length=255, unique=True, null=False)
Numeric Precision & Scale
For defining numeric columns with specific precision and scale.
JSON Column for File Metadata
Stores structured JSON data, particularly useful for file storage references.