常见问题

SQLAlchemy 1.4 supports asyncio, what will GINO be?

Starting from 1.4, SQLAlchemy will support asyncio. This is a great news to the SQLAlchemy-based ORMs including GINO, because the users will have one more option, and many GINO hacks can be eventually cleaned up. SQLAlchemy achieved both sync and async API with the same code base by encapsulating the use of greenlet. As an “async” user, you don’t need to worry about its internals in most cases, it’s fine to just use its async APIs. Both SQLAlchemy Core and ORM will be async-compatible, but some ORM features that involve implicit database accesses are disallowed.

Given such news, GINO no longer has to maintain its own asyncpg dialect for SQLAlchemy in future versions (yay!). Still, GINO will focus on the differences and keep a compatible API. To list some of the different/future features:

  • Contextual Connections (see 引擎与连接)

  • SQLAlchemy Core-based CRUD models

  • The GINO Loader system

  • Async MySQL support

  • Typing support NEW

  • Trio support NEW

  • Execution performance NEW

As SQLAlchemy async support is considered in Alpha level, GINO will include SQLAlchemy 1.4 support in GINO 2.0.0-alpha.x releases, while the current GINO 1.0.x and 1.1.x will remain on SQLAlchemy 1.3. GINO 1.x will receive bug fixes and security updates until both SQLAlchemy async support and GINO 2.x are stabilized.

ORM or not ORM?

GINO does perform the Object-Relational Mapping work under the Data Mapper Pattern, but it is just not a traditional ORM. The Objects in GINO are completely stateless from database - they are pure plain Python objects in memory. Changing their attribute values does not make them “dirty” - or in a different way of thinking they are always “dirty”. Any access to database must be explicitly executed. Using GINO is more like making up SQL clauses with Models and Objects, executing them to make changes in database, or loading data from database and wrapping the results with Objects again. Objects are just row data containers, you are still dealing with SQL which is represented by Models and SQLAlchemy core grammars. Besides GINO can be used in a completely non-ORM way.

Can I use features of SQLAlchemy ORM?

SQLAlchemy has two parts:

  • SQLAlchemy Core

  • SQLAlchemy ORM

GINO is built on top of SQLAlchemy Core, so SQLAlchemy ORM won’t work in GINO.

How to join?

GINO invented none about making up queries, everything for that is inherited from SQLAlchemy. Therefore you just need to know how to write join in SQLAlchemy. Especially, Ádám made some amazing upgrades in GINO #113 to make join easier, so that you can use model classes directly as if they are tables in joining:

  1. results = await User.join(Book).select().gino.all()

How to connect to database through SSL?

It depends on the dialect and database driver. For asyncpg, keyword arguments on asyncpg.connect() are directly available on create_engine() or db.set_bind(). Therefore, enabling SSL is rather easy:

  1. engine = await gino.create_engine(..., ssl=True)

What is aiocontextvars and what does it do?

It is a partial backport of the new built-in module contextvars introduced in Python 3.7. In Python 3.5 and 3.6, aiocontextvars patches loop.create_task() to copy context from caller as a workaround to simulate the same behavior. This is also under discussion in upstream backport project, please read more here: https://github.com/MagicStack/contextvars/issues/2

If you are using Python 3.7, then aiocontextvars does nothing at all.

注解

This answer is for GINO 0.8 and later, please check earlier versions of this documentation if you are using GINO 0.7.

How to define relationships?

GINO 1.0 or lower doesn’t provide relationship definition feature found in typical ORMs. However, you could always manually define your tables, design your queries and load the results explicitly in GINO. Please see 加载器与关系 for more information.

How to define index with multiple columns?

  1. class User(db.Model):
  2. __tablename__ = 'users'
  3. first_name = db.Column(db.Unicode())
  4. last_name = db.Column(db.Unicode())
  5. _name_idx = db.Index('index_on_name', 'first_name', 'last_name')

The _name_idx is not used.

Is there a django admin interface for GINO?

Not quite yet, please follow this discussion.

How to use multiple databases for different users on the fly?

GINO models are linked to a Gino instance, while Gino has an optional property bind to hold a GinoEngine instance. So when you are executing:

  1. user = await User.get(request.user_id)

The bind is implicitly used to execute the query.

In order to use multiple databases, you would need multiple GinoEngine instances. Here’s a full example using FastAPI with lazy engine creation:

  1. from asyncio import Future
  2. from contextvars import ContextVar
  3. from fastapi import FastAPI, Request
  4. from gino import create_engine
  5. from gino.ext.starlette import Gino
  6. engines = {}
  7. dbname = ContextVar("dbname")
  8. class ContextualGino(Gino):
  9. @property
  10. def bind(self):
  11. e = engines.get(dbname.get(""))
  12. if e and e.done():
  13. return e.result()
  14. else:
  15. return self._bind
  16. @bind.setter
  17. def bind(self, val):
  18. self._bind = val
  19. app = FastAPI()
  20. db = ContextualGino(app)
  21. @app.middleware("http")
  22. async def lazy_engines(request: Request, call_next):
  23. name = request.query_params.get("db", "postgres")
  24. fut = engines.get(name)
  25. if fut is None:
  26. fut = engines[name] = Future()
  27. try:
  28. engine = await create_engine("postgresql://localhost/" + name)
  29. except Exception as e:
  30. fut.set_exception(e)
  31. del engines[name]
  32. raise
  33. else:
  34. fut.set_result(engine)
  35. await fut
  36. dbname.set(name)
  37. return await call_next(request)
  38. @app.get("/")
  39. async def get():
  40. return dict(dbname=await db.scalar("SELECT current_database()"))

How to load complex query results?

The API doc of gino.loader explains the available loaders, and there’re a few examples in 加载器与关系 too.

Below is an example with a joined result to load both a GINO model and an integer at the same time, using a TupleLoader with two sub-loaders - ModelLoader and ColumnLoader:

  1. import asyncio
  2. import random
  3. import string
  4. import gino
  5. from gino.loader import ColumnLoader
  6. db = gino.Gino()
  7. class User(db.Model):
  8. __tablename__ = 'users'
  9. id = db.Column(db.Integer(), primary_key=True)
  10. name = db.Column(db.Unicode())
  11. class Visit(db.Model):
  12. __tablename__ = 'visits'
  13. id = db.Column(db.Integer(), primary_key=True)
  14. time = db.Column(db.DateTime(), server_default='now()')
  15. user_id = db.Column(db.ForeignKey('users.id'))
  16. async def main():
  17. async with db.with_bind('postgresql://localhost/gino'):
  18. await db.gino.create_all()
  19. for i in range(random.randint(5, 10)):
  20. u = await User.create(
  21. name=''.join(random.choices(string.ascii_letters, k=10)))
  22. for v in range(random.randint(10, 20)):
  23. await Visit.create(user_id=u.id)
  24. visits = db.func.count(Visit.id)
  25. q = db.select([
  26. User,
  27. visits,
  28. ]).select_from(
  29. User.outerjoin(Visit)
  30. ).group_by(
  31. *User,
  32. ).gino.load((User, ColumnLoader(visits)))
  33. async with db.transaction():
  34. async for user, visits in q.iterate():
  35. print(user.name, visits)
  36. await db.gino.drop_all()
  37. asyncio.run(main())

Be ware of the tuple in .gino.load((...)).

How to do bulk or batch insert / update?

For a simple example, take a model that has one field, “name.” In your application you have a list of names you would like to add to the database:

  1. new_names = ["Austin", "Ali", "Jeff", "Marissa"]

To quickly insert the names in one query, first construct a dict with the {"model_key": "value"} format:

  1. new_names_dict = [dict(name=new_name) for new_name in new_names]
  2. >> [{'name': 'Austin'}, {'name': 'Ali'}, {'name': 'Jeff'}, {'name': 'Marissa'}]

Finally, run an insert statement on the model:

  1. await User.insert().gino.all(new_names_dict)

How to print the executed SQL?

GINO uses the same approach from SQLAlchemy: create_engine(..., echo=True). (Or db.set_bind(..., echo=True)) Please see also here.

If you use any extension, you can also set that in config, by db_echo or DB_ECHO.

How to run EXISTS SQL?

  1. await db.scalar(db.exists().where(User.email == email).select())

How to work with Alembic?

The fact that Gino is a MetaData is the key to use Alembic. Just import and set target_metadata = db in Alembic env.py will do. See 使用 Alembic for more details.

How to join the same table twice?

This is the same pattern as described in SQLAlchemy Adjacency List Relationships, where you have a table with “a foreign key reference to itself”, or join the same table more than once, “to represent hierarchical data in flat tables.” We’d need to use alias(), for example:

  1. class User(db.Model):
  2. __tablename__ = "users"
  3. id = db.Column(db.Integer, primary_key=True)
  4. parent_id = db.Column(db.ForeignKey("users.id"))
  5. Parent = User.alias()
  6. query = User.outerjoin(Parent, User.parent_id == Parent.id).select()
  7. users = await query.gino.load(User.load(parent=Parent)).all()

How to execute raw SQL with parameters?

Wrap the SQL with text(), and use keyword arguments:

  1. query = db.text('SELECT * FROM users WHERE id = :id_val')
  2. row = await db.first(query, id_val=1)

You may even load the rows into model instances:

  1. query = query.execution_options(loader=User)
  2. user = await db.first(query, id_val=1)

Gino engine is not initialized?

GINO models are linked to a Gino instance, while Gino has an optional property bind to hold a GinoEngine instance. So when you are executing:

  1. user = await User.get(request.user_id)

The bind is implicitly used to execute the query. If bind is not set before this, you’ll see this error:

  1. gino.exceptions.UninitializedError: Gino engine is not initialized.

You could use either:

  • Call set_bind() or with_bind() to set the bind on the Gino instance.

  • Use one of the Web framework extensions to set the bind for you in usually the server start-up hook.

  • Use explicit bind for each execution, for example:

    1. engine = await create_engine("...")
    2. # ...
    3. user = await User.get(request.user_id, bind=engine)

How can I do SQL xxxx in GINO?

GINO uses SQLAlchemy Core queries, so please check its documentation on how to build queries. The GINO models are simply wrappers of SQLAlchemy Table instances, and the column attributes on GINO model classes are just SQLAlchemy Column instances, you can use them in building your SQLAlchemy Core queries.

Alternatively, you could always execute the raw SQL directly, see How to execute raw SQL with parameters? above.