Column Class

In the context of a database table, each property marked as a column in a model is treated as an individual attribute. Here’s an example of how to define a column in a table using the Column class

username = Column(type="text", nullable=False, default="Hello there!!")

Here are some other options that you can pass to the Column

Argument

Description

Type

Default

type

Required datatype of a column.

any

nullable

Optional to specify if the column will allow null values or not.

bool

False

length

Optional to specify the length of the type. If passed as N with type T, it yields an SQL statement with type T(N).

None

None

auto_increment

Optional to specify if the column will automatically increment or not.

bool

False

default

Optional to specify the default value in a column.

any

None

unique

Optional to specify if the column will contain unique values or not.

bool

False

Note

Talking about data types, each dialect has its own accepted values. Here is a list of types supported by each and every dialect:

Column Data types

In this section we will list all the datatypes that are supported for each dialect.

  1. mysql

Data Type

Description

"int"

Integer data type.

"smallint"

Small integer data type.

"bigint"

Big integer data type.

"float"

Floating-point number data type.

"double"

Double-precision floating-point number data type.

"numeric"

Numeric or decimal data type.

"text"

Text data type.

"varchar"

Variable-length character data type.

"char"

Fixed-length character data type.

"boolean"

Boolean data type.

"date"

Date data type.

"time"

Time data type.

"timestamp"

Timestamp data type.

"json"

JSON (JavaScript Object Notation) data type.

  1. postgres

Data Type

Description

"int"

Integer data type (Alias: "INTEGER").

"smallint"

Small integer data type (Alias: "SMALLINT").

"bigint"

Big integer data type (Alias: "BIGINT").

"serial"

Auto-incrementing integer data type (Alias: "SERIAL").

"bigserial"

Auto-incrementing big integer data type (Alias: "BIGSERIAL").

"smallserial"

Auto-incrementing small integer data type (Alias: "SMALLSERIAL").

"float"

Real number data type (Alias: "REAL").

"double precision"

Double-precision floating-point number data type (Alias: "DOUBLE PRECISION").

"numeric"

Numeric data type (Alias: "NUMERIC").

"text"

Text data type.

"varchar"

Variable-length character data type.

"char"

Fixed-length character data type.

"boolean"

Boolean data type.

"date"

Date data type.

"time"

Time data type.

"timestamp"

Timestamp data type.

"interval"

Time interval data type.

"uuid"

UUID (Universally Unique Identifier) data type.

"json"

JSON (JavaScript Object Notation) data type.

"jsonb"

Binary JSON (JavaScript Object Notation) data type.

"bytea"

Binary data type (Array of bytes).

"array"

Array data type.

"inet"

IP network address data type.

"cidr"

Classless Inter-Domain Routing (CIDR) address data type.

"macaddr"

MAC (Media Access Control) address data type.

"tsvector"

Text search vector data type.

"point"

Geometric point data type.

"line"

Geometric line data type.

"lseg"

Geometric line segment data type.

"box"

Geometric box data type.

"path"

Geometric path data type.

"polygon"

Geometric polygon data type.

"circle"

Geometric circle data type.

"hstore"

Key-value pair store data type.

  1. sqlite

Data Type

Description

"int"

Integer data type.

"smallint"

Small integer data type.

"bigint"

Big integer data type.

"float"

Real number data type.

"double precision"

Double-precision floating-point number data type.

"numeric"

Numeric data type.

"text"

Text data type.

"varchar"

Variable-length character data type.

"char"

Fixed-length character data type.

"boolean"

Boolean data type.

"date"

Date data type.

"time"

Time data type.

"timestamp"

Timestamp data type.

"json"

JSON (JavaScript Object Notation) data type.

Note

Every table that is not a joint_table is required to have a primary key column and this column should be 1. Let’s talk about the PrimaryKeyColumn