|
|||||||
Persisting the Tables / создание таблиц использую metadata
Время создания: 20.09.2017 11:47
Текстовые метки: knowledge
Раздел: SQLAlchemy
Запись: xintrea/mytetra_db_mcold/master/base/15058972729oof4rsxu2/text.html на raw.githubusercontent.com
|
|||||||
|
|||||||
All of our tables and additional schema definitions are associated with an instance of metadata. Persisting the schema to the database is simply a matter of calling the create_all() method on our metadata instance with the engine where it should cre‐ ate those tables: metadata.create_all(engine) By default, create_all will not attempt to re-create tables that already exist in the database, and it is safe to run multiple times. It’s wiser to use a database migration tool like Alembic to handle any changes to existing tables or additional schema than to try to handcode changes directly in your application code (we’ll explore this more fully in Chapter 11). Now that we have persisted the tables in the database, let’s take a look at Example 1-4, which shows the complete code for the tables we’ve been work‐ ing on in this chapter. from datetime import datetime In this chapter, we took a look at how metadata is used as a catalog by SQLAlchemy to store table schemas along with other miscellaneous data. We also can define a table with multiple columns and constraints. We explored the types of constraints and how to explicitly construct them outside of a column object to match an existing schema or naming scheme. Then we covered how to set default values and onupdate values for auditing. Finally, we now know how to persist or save our schema into the database for reuse. The next step is to learn how to work with data within our schema via the SQL Expression Language. |
|||||||
Так же в этом разделе:
|
|||||||
|
|||||||
|