Skip to content

Commit 9ba902b

Browse files
committed
Add "String fields and column length" sections to docs
1 parent 5611bda commit 9ba902b

11 files changed

Lines changed: 217 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# String fields and column length
2+
3+
Some databases have a limit on the length of string columns (e.g., `VARCHAR(255)` in *MySQL*) and fail with an error if you try to create a string column without specifying a length.
4+
5+
**SQLModel** handles this automatically depending on the database dialect you are using. 😎
6+
7+
For databases that require a length for string columns, **SQLModel** will automatically set a default length (e.g., `255` for *MySQL*) if you do not specify one.
8+
9+
{* ./docs_src/tutorial/str_fields_and_column_length/tutorial001_py310.py ln[4:6] hl[6] *}
10+
11+
If you run this code with *MySQL*, **SQLModel** will create the `name` column as `VARCHAR(255)`:
12+
13+
```sql
14+
CREATE TABLE hero (
15+
id INTEGER NOT NULL AUTO_INCREMENT,
16+
name VARCHAR(255) NOT NULL,
17+
PRIMARY KEY (id)
18+
)
19+
```
20+
21+
But you can always specify a custom length if needed:
22+
23+
{* ./docs_src/tutorial/str_fields_and_column_length/tutorial002_py310.py ln[4:6] hl[6] *}
24+
25+
```sql
26+
CREATE TABLE hero (
27+
id INTEGER NOT NULL AUTO_INCREMENT,
28+
name VARCHAR(100) NOT NULL,
29+
PRIMARY KEY (id)
30+
)
31+
```
32+
This works thanks to `AutoString` type that **SQLModel** uses for all string fields by default.
33+
34+
But if you specify the database type of column explicitly, **SQLModel** will not be able to set the length automatically, and you will need to specify it manually:
35+
36+
{* ./docs_src/tutorial/str_fields_and_column_length/tutorial003_py310.py ln[1:6] hl[1,6] *}
37+
38+
The code example above will fail on databases that require a length for string columns:
39+
40+
```console
41+
sqlalchemy.exc.CompileError: (in table 'hero', column 'name'): VARCHAR requires a length on dialect mysql
42+
```
43+
44+
To fix it, you need to specify the length explicitly as follows:
45+
46+
{* ./docs_src/tutorial/str_fields_and_column_length/tutorial004_py310.py ln[1:6] hl[1,6] *}
47+
48+
This will give:
49+
50+
```sql
51+
CREATE TABLE hero (
52+
id INTEGER NOT NULL AUTO_INCREMENT,
53+
name VARCHAR(255) NOT NULL,
54+
PRIMARY KEY (id)
55+
)
56+
```

docs_src/tutorial/str_fields_and_column_length/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlmodel import Field, SQLModel, create_engine
2+
3+
4+
class Hero(SQLModel, table=True):
5+
id: int | None = Field(default=None, primary_key=True)
6+
name: str
7+
8+
9+
database_url = "mysql://user:password@localhost/dbname"
10+
11+
engine = create_engine(database_url, echo=True)
12+
13+
14+
def create_db_and_tables():
15+
SQLModel.metadata.create_all(engine)
16+
17+
18+
if __name__ == "__main__":
19+
create_db_and_tables()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, SQLModel, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str
9+
10+
11+
database_url = "mysql://user:password@localhost/dbname"
12+
13+
engine = create_engine(database_url, echo=True)
14+
15+
16+
def create_db_and_tables():
17+
SQLModel.metadata.create_all(engine)
18+
19+
20+
if __name__ == "__main__":
21+
create_db_and_tables()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlmodel import Field, SQLModel, create_engine
2+
3+
4+
class Hero(SQLModel, table=True):
5+
id: int | None = Field(default=None, primary_key=True)
6+
name: str = Field(max_length=100)
7+
8+
9+
database_url = "mysql://user:password@localhost/dbname"
10+
11+
engine = create_engine(database_url, echo=True)
12+
13+
14+
def create_db_and_tables():
15+
SQLModel.metadata.create_all(engine)
16+
17+
18+
if __name__ == "__main__":
19+
create_db_and_tables()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, SQLModel, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str = Field(max_length=100)
9+
10+
11+
database_url = "mysql://user:password@localhost/dbname"
12+
13+
engine = create_engine(database_url, echo=True)
14+
15+
16+
def create_db_and_tables():
17+
SQLModel.metadata.create_all(engine)
18+
19+
20+
if __name__ == "__main__":
21+
create_db_and_tables()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlmodel import Field, SQLModel, String, create_engine
2+
3+
4+
class Hero(SQLModel, table=True):
5+
id: int | None = Field(default=None, primary_key=True)
6+
name: str = Field(sa_type=String)
7+
8+
9+
database_url = "mysql://user:password@localhost/dbname"
10+
11+
engine = create_engine(database_url, echo=True)
12+
13+
14+
def create_db_and_tables():
15+
SQLModel.metadata.create_all(engine)
16+
17+
18+
if __name__ == "__main__":
19+
create_db_and_tables()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, SQLModel, String, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str = Field(sa_type=String)
9+
10+
11+
database_url = "mysql://user:password@localhost/dbname"
12+
13+
engine = create_engine(database_url, echo=True)
14+
15+
16+
def create_db_and_tables():
17+
SQLModel.metadata.create_all(engine)
18+
19+
20+
if __name__ == "__main__":
21+
create_db_and_tables()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlmodel import Field, SQLModel, String, create_engine
2+
3+
4+
class Hero(SQLModel, table=True):
5+
id: int | None = Field(default=None, primary_key=True)
6+
name: str = Field(sa_type=String(length=255))
7+
8+
9+
database_url = "mysql://user:password@localhost/dbname"
10+
11+
engine = create_engine(database_url, echo=True)
12+
13+
14+
def create_db_and_tables():
15+
SQLModel.metadata.create_all(engine)
16+
17+
18+
if __name__ == "__main__":
19+
create_db_and_tables()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import Optional
2+
3+
from sqlmodel import Field, SQLModel, String, create_engine
4+
5+
6+
class Hero(SQLModel, table=True):
7+
id: Optional[int] = Field(default=None, primary_key=True)
8+
name: str = Field(sa_type=String(length=255))
9+
10+
11+
database_url = "mysql://user:password@localhost/dbname"
12+
13+
engine = create_engine(database_url, echo=True)
14+
15+
16+
def create_db_and_tables():
17+
SQLModel.metadata.create_all(engine)
18+
19+
20+
if __name__ == "__main__":
21+
create_db_and_tables()

0 commit comments

Comments
 (0)