-3

Есть существующая БД на PostgreSQL. С помощью SQLAlchemy я реализовал схему этой БД.

Есть ли способ средствами SQLAlchemy (или отдельный пакет Python) проверить, не допустил ли я ошибку при описании схемы?

Интерисует все: имена таблиц, столбцов (их тип и длина), первичные и внешние ключи.

1

2 ответа 2

1

Проще будет при построении схемы воспользоваться метаданными из БД, использовав DB object reflection:

messages = Table('messages', meta, autoload=True, autoload_with=engine)
[c.name for c in messages.columns]
#['message_id', 'message_name', 'date']

Ограничения:

Limitations of Reflection

It’s important to note that the reflection process recreates Table metadata using only information which is represented in the relational database. This process by definition cannot restore aspects of a schema that aren’t actually stored in the database. State which is not available from reflection includes but is not limited to:

Client side defaults, either Python functions or SQL expressions defined using the default keyword of Column (note this is separate from server_default, which specifically is what’s available via reflection). Column information, e.g. data that might have been placed into the Column.info dictionary The value of the .quote setting for Column or Table The association of a particular Sequence with a given Column The relational database also in many cases reports on table metadata in a different format than what was specified in SQLAlchemy. The Table objects returned from reflection cannot be always relied upon to produce the identical DDL as the original Python-defined Table objects. Areas where this occurs includes server defaults, column-associated sequences and various idosyncrasies regarding constraints and datatypes. Server side defaults may be returned with cast directives (typically PostgreSQL will include a :: cast) or different quoting patterns than originally specified.

Another category of limitation includes schema structures for which reflection is only partially or not yet defined. Recent improvements to reflection allow things like views, indexes and foreign key options to be reflected. As of this writing, structures like CHECK constraints, table comments, and triggers are not reflected.

2
0

Используйте sqlacodegen.

Использовать очень просто - sqlacodegen postgresql:///some_local_db > file.txt

Выгружает в файл полную модель БД со всеми связями

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.