Advice for unique=True, max_length=30, min_length=3, that are causing a few problems #1132
Unanswered
tomislavm021
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
unique=True, max_length=30, min_length=3 are not working in sqlmodel
Operating System
Windows
Operating System Details
No response
SQLModel Version
sqlmodel==0.0.22
Python Version
3.12
Additional Context
I am sorry if i didn't fill in the notes correctly. I am coming from django and currently learning about sqlmodel and fastapi. I love it! I think it is great, but i run into a few problems. I am coding along with one udemy course.
The author creates the database.py
`from sqlmodel import SQLModel, create_engine, Field, Session
import models
sqlite_filename = 'database.db'
sqlite_url = f"sqlite:///{sqlite_filename}"
engine = create_engine(sqlite_url, echo=True)
if name == 'main':
SQLModel.metadata.create_all(engine)
`
This is models.py
`from sqlmodel import SQLModel, Field
from typing import Optional
class Category(SQLModel, table=True):
id: Optional[int] = Field(primary_key=True, default=None)
name: str = Field(index=True, unique=True, max_length=30, min_length=3)`
This is main.py
`
from fastapi import FastAPI
from sqlmodel import SQLModel, Session
from models import Category, CreateCategory
from database import engine
from fastapi.responses import HTMLResponse
define main app name
app = FastAPI()
define database session name
session = Session(bind=engine)
@app.post('/category/create')
async def create_category(category:Category):
new_category = Category(name=category.name)
with Session(engine) as session:
session.add(new_category)
session.commit()
session.refresh(new_category)
return new_category`
Now in the docs i saw that the version 0.0.7 has the unique validation working but i tried it and it doesn't, so i am sure the mistake is somewhere on my side.
So my code is the same. But i don't get validations for unique=True, max_length=30, min_length=3, and he does.
I also tried to create one model for creating the object, like this:
class CreateCategory(SQLModel): name: str = Field(unique=True, max_length=30, min_length=3, default='demo')
Then i use that in the async def with category:CreateCategory and now the min and max validations are working, but the unique=True still doesn't work.
So i am confused, i can't continue with the learning process until i learn this. If sqlmodel is to shorten the development time and act as a database model and validation model, why is there a need to create additional schemas for validation, especially if they are not working / not validating? Why can't we just use sqlalchemy and pydantic if there is a problem with sqlmodel?
Can someone please help explain this?
Thank you
p.s. in the field above, i wrote a User model based on my Django projects.
Beta Was this translation helpful? Give feedback.
All reactions