Git Product home page Git Product logo

Comments (9)

art049 avatar art049 commented on May 20, 2024 18

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.

ecosys-technology avatar ecosys-technology commented on May 20, 2024 6

This is such an important feature, hopefully the devs will be able to add it soon πŸ‘

from odmantic.

ehiggs avatar ehiggs commented on May 20, 2024 1

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.

mattyweb avatar mattyweb commented on May 20, 2024

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.

rdfsx avatar rdfsx commented on May 20, 2024

So, until that is done, what is the best practice for creating indexes? Where is it better to create them?

from odmantic.

ehiggs avatar ehiggs commented on May 20, 2024

@rdfsx https://github.com/stxnext/pymongo-migrate

from odmantic.

schwannden avatar schwannden commented on May 20, 2024

such an important feature, plz have it soon

from odmantic.

rdfsx avatar rdfsx commented on May 20, 2024

@rdfsx https://github.com/stxnext/pymongo-migrate

So where should I look?:)

from odmantic.

rdfsx avatar rdfsx commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.