1. Create an instance of the mapping class (Instance)
The previous section describes how to map a database entity table to a Python class, below we can create an instance (Instance) of this class, let's take the User class from the previous article as an example, let's create the User object:
>>> ed_user = User('ed', 'Ed Jones', 'edspassword')
>>> ed_user.name
'ed'
>>> ed_user.password
'edspassword'
>>> str(ed_user.id)
'None'
Instantiated as a normal Python class, you may ask why ed_user.id will be None value, first of all, the property id is not initialized by the __init__() constructor, so by default it will have a None value because of the previously defined id column (Column) of the ORM, by default the ORM creates class properties for all the mapped By default, the ORM creates class attributes for all mapped columns, which are implemented through the Descriptors mechanism in the Python language. So the use of these properties will include some additional behavior, including tracking modifications, or automatically loading new data from the database when needed, which means that we will trigger a series of actions within the ORM when we use these properties, including modifying or reading them.
Wait, you haven't said why the id attribute will be None value. Oh, in fact, we do not insert the data into the database now, generally the primary key attribute will be inserted into the database automatically generate a non-repeating value to ensure uniqueness. Because we do not implement persistence (Persist) (the so-called persistence is the object data in accordance with the mapping relationship stored in the database) so the id value is None. do not worry, later when we introduce the data will be persistent after you can see a new auto-generated id.
Next up, a little lazy tip for a lazy trick :-)
What would happen if we didn't define the constructor __init__() for the mapping class? Not at all, SQLAlchemy takes this into account for us if we are lazy and define the previous User class like this:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
password = Column(String)
Since User inherits from Base (see previous article for Base definition), it is managed by the Declarative system, which found that this class lacks a constructor method, so it is very friendly to give us a constructor method, of course, the constructor method provided by it can't be accessed by position-based parameters like the constructor method we defined ourselves. We suggest to use key-based parameter access, including all the columns we use Column to define the mapping of columns, such as the following way:
u1 = User(name='ed', fullname='Ed Jones', password='foobar')
The id can also be passed in, in the usual sense that this type of primary key is automatically maintained by the system and we don't need to assign a value to it.
2. Creation and use of sessions
Here can be said to be "everything is ready, all we need is the east wind", with the words of the official document "We're now ready to start 'talking' to the database" (We're now ready to start talking to the database). We're now ready to start talking to the database" (We're now ready to start talking to the database). The handle of the ORM is called a session. In order to use a session, we need to configure it first. The code statement to configure the session should be at the same code level as the code statement to create_engine() to create the engine (just put it together).
For example, we use create_engine() to first create the engine name engine (you can refer to my first article for the code to create the engine), and then use the sessionmaker() factory function to create the Session class, and at the same time bind our existing engine, for example, the code is as follows:
>>> from import sessionmaker
>>> Session = sessionmaker(bind=engine)
What if the code that creates the session is not at the same level as the code that creates the engine, for example, sessionmaker() creates a session class, and then creates the engine with create_engine(), do we still have a chance to bind the session and the engine together? Of course we can, we can use the configure method of the Session class to configure the engine binding like this:
Session = sessionmaker()
# engine = create_engine(...) Creating an engine
(bind=engine) # The engine should have been created by now.
By now the Session class created by the sessionmaker() factory should be bound to the Engine we created earlier, but the session hasn't really started yet, to start the session we need to instantiate the Session class:
>>> session = Session()
Here the session acquires a pool of database connections maintained by the Engine and maintains the mapped data in memory until changes are committed or the session object is closed.
Here the session creation is explained, the next part of the real ORM database query will be explained, welcome to follow!