Comments (9)
It's not yet possible, here is the current draft of the feature π
Unique fields
class Tree(Model):
name: str = Field(unique=True)
Regular index
class Tree(Model):
name: str = Field(index=True)
Compound indexes
class Book(Model):
name: str
pages: int
class Config:
def indexes():
yield Index(cls.name, cls.pages)
I would be glad to have some feedback on this draft !
from odmantic.
This is such an important feature, hopefully the devs will be able to add it soon π
from odmantic.
First, when using sqlalchemy do people actually use the indexing properties or do they make all the tables using alembic? Or is this like 'you /can/ make the tables in code just like you /can/ make a requirements.txt without pinning versions'. I lean to the side of doing all the schema work in the migration file(s) (cf diesel.rs) so that's where I'm coming from.
In my experience when you have the index defined by the ORM then lifecycle management (aka migrations) are painful when you realise an index is no longer doing it's job and you want to remove it. It's also painful to update indexes when you want to have two indexes on the same criteria (using different partial filter expressions). (This is fixed in mongo5 π )
Also you cannot ALTER indexes in mongo which means adding new fields with index properties either needs to fail on startup (π¬ ) or add a new index on the fly (π± )
Of course who wants to spend the time and effort re-writing alembic for Mongo when it has no schemas to migrate? Well in this case you do want a migration tool for indexes rather than doing this in code in my humble opinion.
Curious to know what you think!
from odmantic.
I like the feature and the design, but one bit of feedback on this feature is that I often make indexes using partialFilterExpression syntax to get more fine-grained nullability.
I'm not the Mongo expert here but as I understand it this is the best way to ensure that an index is unique when it also needs to be nullable. Otherwise, you'd get an error if you tried to add more than 1 null value.
Here's an example:
users.create_index(
"email",
unique=True,
partialFilterExpression={"email": {"$type": "string"}},
)
I'd typically do this in a FastAPI app where I want users to either have no email address or a unique one. This lets me surface a 409 error when there's a document conflict.
Maybe you could add a nullable=True
option to Field as well?
from odmantic.
So, until that is done, what is the best practice for creating indexes? Where is it better to create them?
from odmantic.
@rdfsx https://github.com/stxnext/pymongo-migrate
from odmantic.
such an important feature, plz have it soon
from odmantic.
So where should I look?:)
from odmantic.
such an important feature, plz have it soon
Perhaps I could do a pull request, but I need some explanation π€
from odmantic.
Related Issues (20)
- Embedded Model Exclude None Not Functioning HOT 1
- Support native validator
- Using the union operator in model breaks HOT 1
- FastAPI schema can not show the fields in parent odmantic.Model class
- Add support for Pydantic V2 HOT 13
- Odmantic model names in openapi.json from FastAPI have __pydantic_model__ suffix
- ζδΉζεΊ
- Pydantic URLs have stopped working
- Support other document store
- Using deprecated `pydantic.datetime_parse:parse_datetime` in Pydantic HOT 1
- Can't start dev container: multi-python is deprecated
- Optional EmbeddedModel not working HOT 3
- Just started following tutorial in https://art049.github.io/odmantic/ and an error HOT 3
- Any specific reason to not support model inheritance?
- `Optional[str]` triggers `int_parsing` instead of string parsing when used for `odmantic.Model` HOT 4
- Multiple sorting criteria HOT 1
- Mypy issues for odmantic 1.0.0 HOT 2
- Trying to use db_engine.configure_database([User]) HOT 1
- Support Inheritance
- regex is removed in pydantic 2.x this causes the Field class to throw an exception if its used. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from odmantic.