zoukankan      html  css  js  c++  java
  • peewee a django style ORM

    django model

    https://docs.djangoproject.com/en/3.2/topics/db/models/

    django的ORM工具, 具有非常简洁的接口,遵从django快速开发的理念。

    但是此工具是跟django深度绑定的, 很难独立使用。

    A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

    The basics:

    • Each model is a Python class that subclasses django.db.models.Model.
    • Each attribute of the model represents a database field.
    • With all of this, Django gives you an automatically-generated database-access API; see Making queries.
    from django.db import models
    
    class Person(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=30)

    https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

    Django's ORM

    Pros:

    1. Easy-to-use with a short learning curve
    2. Tightly integrated with Django to make it the de-factor standard when dealing with databases in Django

    Cons:

    1. Does not handle complex queries very well; forcing the developer to go back to raw SQL
    2. Tightly integrated with Django; making it hard to use outside of a Django context

    SQLAlchemy

    https://www.sqlalchemy.org/

    企业级持久化套件, 设计上高效和高性能的数据库访问。

    SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

    It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

    https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

    API比较重量,学习曲线长。

    Pros:

    1. Enterprise-level APIs; making the code robust and adaptable
    2. Flexible design; making it painless to write complex queries

    Cons:

    1. The Unit-of-work concept is not common
    2. A heavyweight API; leading to a long learning curve

    migration

    https://alembic.sqlalchemy.org/en/latest/autogenerate.html

    peewee

    https://stackoverflow.com/questions/53428/what-are-some-good-python-orm-solutions

    正解: 拥有django风格。

    If you're looking for lightweight and are already familiar with django-style declarative models, check out peewee: https://github.com/coleifer/peewee

    import datetime
    from peewee import *
    
    class Blog(Model):
        name = CharField()
    
    class Entry(Model):
        blog = ForeignKeyField(Blog)
        title = CharField()
        body = TextField()
        pub_date = DateTimeField(default=datetime.datetime.now)
    
    # query it like django
    Entry.filter(blog__name='Some great blog')
    
    # or programmatically for finer-grained control
    Entry.select().join(Blog).where(Blog.name == 'Some awesome blog')

    https://www.pythoncentral.io/sqlalchemy-vs-orms/#:~:text=%20Comparison%20Between%20Python%20ORMs%20%201%20SQLObject.,the%20unit-of-work%20concept%20which%20is%20prevalent...%20More%20

    轻量,易于集成。

    Pros:

    1. A Django-ish API; making it easy-to-use
    2. A lightweight implementation; making it easy to integrate with any web framework

    Cons:

    1. Does not support automatic schema migrations
    2. Many-to-Many queries are not intuitive to write

    Reference

    https://github.com/coleifer/peewee

    http://docs.peewee-orm.com/en/latest/peewee/quickstart.html

    DB migration

    peewee built-in prestage

    http://docs.peewee-orm.com/en/2.10.2/peewee/playhouse.html#migrate

    peewee推荐的 migration方案

    缺点:

    跟模型隔离, 需要单独维护一份脚本。

    Schema Migrations

    Peewee now supports schema migrations, with well-tested support for Postgresql, SQLite and MySQL. Unlike other schema migration tools, peewee’s migrations do not handle introspection and database “versioning”. Rather, peewee provides a number of helper functions for generating and running schema-altering statements. This engine provides the basis on which a more sophisticated tool could some day be built.

    Migrations can be written as simple python scripts and executed from the command-line. Since the migrations only depend on your applications Database object, it should be easy to manage changing your model definitions and maintaining a set of migration scripts without introducing dependencies.

    from playhouse.migrate import *
    
    
    # SQLite example:
    my_db = SqliteDatabase('my_database.db')
    migrator = SqliteMigrator(my_db)
    
    
    title_field = CharField(default='')
    status_field = IntegerField(null=True)
    
    migrate(
        migrator.add_column('some_table', 'title', title_field),
        migrator.add_column('some_table', 'status', status_field),
        migrator.drop_column('some_table', 'old_column'),
    )

    peewee-db-evolve

    https://github.com/keredson/peewee-db-evolve

    优点:

    根据模型同步更新数据库表。

    缺点:

    只支持两种数据库 mysql和postgre,

    不支持sqlite

    Peewee DB Evolve

    Diffs your models against your database, and outputs SQL to (non-destructively) update your schema.

    Think of it as db.create_tables() on steriods (which doesn't drop your database).

    You can also think of it as schema migrations, without having to actually write the migrations.

    使用样例

    https://github.com/keredson/peewee-db-evolve/tree/master/examples/hello_world

    peewee_migrate

    https://stackoverflow.com/questions/24906683/can-flask-peewee-do-migration

    https://github.com/klen/peewee_migrate

    文档不友好, 不清楚是否支持哪些数据库, 是否支持从模型生成migrations文件。

    Simple migration engine for Peewee

    from peewee_migrate import Router
    from peewee import SqliteDatabase
    
    router = Router(SqliteDatabase('test.db'))
    
    # Create migration
    router.create('migration_name')
    
    # Run migration/migrations
    router.run('migration_name')
    
    # Run all unapplied migrations
    router.run()

    peewee_migrations

    https://github.com/aachurin/peewee_migrations

    命令行友好。sqlite not support。

    Migrations for peewee orm.

    出处:http://www.cnblogs.com/lightsong/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    hihocoder 1664
    hihocoder 1654
    javascript高级程序设计学习小结3
    javascript高级程序设计小结2
    javascript高级程序设计学习小结1
    js中原型和原型链理解
    javascript对象封装的常用方式
    vue学习总结2
    vue学习小结1
    JS中一些常用的代码块
  • 原文地址:https://www.cnblogs.com/lightsong/p/15586714.html
Copyright © 2011-2022 走看看