Should I create mapper objects or use the declarative syntax in SQLAlchemy?

前端 未结 4 1304
北恋
北恋 2021-01-30 03:23

There are two (three, but I\'m not counting Elixir, as its not \"official\") ways to define a persisting object with SQLAlchemy:

Explicit syntax for mapper objects

4条回答
  •  别那么骄傲
    2021-01-30 03:53

    "What I'm not completely sure, is which approach is more maintainable for a business application?"

    Can't be answered in general.

    However, consider this.

    The Django ORM is strictly declarative -- and people like that.

    SQLAlchemy does several things, not all of which are relevant to all problems.

    1. SQLAlchemy creates DB-specific SQL from general purpose Python. If you want to mess with the SQL, or map Python classes to existing tables, then you have to use explicit mappings, because your focus is on the SQL, not the business objects and the ORM.

    2. SQLAlchemy can use declarative style (like Django) to create everything for you. If you want this, then you are giving up explicitly writing table definitions and explicitly messing with the SQL.

    3. Elixir is an alternative to save you having to look at SQL.

    The fundamental question is "Do you want to see and touch the SQL?"

    If you think that touching the SQL makes things more "maintainable", then you have to use explicit mappings.

    If you think that concealing the SQL makes things more "maintainable", then you have to use declarative style.

    • If you think Elixir might diverge from SQLAlchemy, or fail to live up to it's promise in some way, then don't use it.

    • If you think Elixir will help you, then use it.

提交回复
热议问题