What is the difference between sqlalchemy core and ORM?

What is the difference between SQLAlchemy Core and SQLAlchemy ORM ?

+6


source to share


2 answers


ORM, as the name suggests, is an object-relational mapper: its purpose is to represent database relationships as Python objects.

The core is a query builder. Its purpose is to provide software tools for generating SQL (and DDL) queries, but the results of those queries are just tuples (with a little extra magic), not your own ("your" being, in this case, "developer design") ...



In general, if you are trying to programmatically create queries (specifically based on information available only at runtime), you should use the kernel. If you are trying to create your MVC-style application and you want database-backed objects to be a "model" then you must use an ORM.

+17


source


SQLAlchemy-ORM is developed on top of SQLAlchemy-Core. As we can see the basic architecture of SQLAlchemy below.

Basic Architecture of SQLAlchemy

So, if we want, we can use SQLAlchemy-Core to execute raw SQL queries after the engine is built . The SQLAlchemy engine object provides a set of methods for performing basic operations like connect (), execute (), etc.



engine = create_engine('mysql://scott:tiger@localhost/test') connection = engine.connect() result = connection.execute("select username from users") for row in result: print("username:", row['username']) connection.close()

If you want ORM functionality from SQLAlchemy, you must use the ORM part. Where will you be able to create Python classes that will be treated like tables and class attributes will be treated like columns. In addition to this, SQLAlchemy will provide many more methods to simplify object-relational mapping.

0


source







All Articles