From fbc4c81faa25d88229e974929323b71aef41a7c8 Mon Sep 17 00:00:00 2001
From: Mike Bayer To start, heres the tables we will work with again:
So that covers how to map the columns in a table to an object, how to load objects, create new ones, and save changes. The next step is how to define an object's relationships to other database-persisted objects. This is done via the relation function provided by the mapper module. So with our User class, lets also define the User has having one or more mailing addresses. First, the table metadata:
<&|formatting.myt:code&> - from sqlalchemy.schema import * - from sqlalchemy.mapper import * - import sqlalchemy.databases.sqlite as sqlite - engine = sqlite.engine('mydb', {}) + from sqlalchemy import * + engine = create_engine('sqlite', {'filename':'mydb'}) # define user table users = Table('users', engine, diff --git a/doc/build/content/dbengine.myt b/doc/build/content/dbengine.myt index d9385c05ed..754b4f71f9 100644 --- a/doc/build/content/dbengine.myt +++ b/doc/build/content/dbengine.myt @@ -16,23 +16,23 @@An example of connecting to each engine is as follows:
<&|formatting.myt:code&> - import sqlalchemy.engine as engine + from sqlalchemy.engine import * # sqlite in memory - sqlite_engine = engine.create_engine('sqlite', {'filename':':memory:'}, **opts) + sqlite_engine = create_engine('sqlite', {'filename':':memory:'}, **opts) # sqlite using a file - sqlite_engine = engine.create_engine('sqlite', {'filename':'querytest.db'}, **opts) + sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'}, **opts) # postgres - postgres_engine = engine.create_engine('postgres', + postgres_engine = create_engine('postgres', {'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}, **opts) # oracle - oracle_engine = engine.create_engine('oracle', + oracle_engine = create_engine('oracle', {'dsn':'mydsn', 'user':'scott', 'password':'tiger'}, **opts) @@ -41,13 +41,15 @@ &>Note that the general form of connecting to an engine is:
<&|formatting.myt:code&> - engine = sqlalchemy.engine.create_engine( + engine = create_engine(The second argument is a dictionary whose key/value pairs will be passed to the underlying DBAPI connect() method as keyword arguments. Any keyword argument supported by the DBAPI module can be in this dictionary.
+An additional URL-string based calling style will also be added soon, as this is a highly requested feature. +
&> <&|doclib.myt:item, name="options", description="Database Engine Options" &>The remaining arguments to create_engine are keyword arguments that are passed to the specific subclass of sqlalchemy.engine.SQLEngine being used, as well as the underlying sqlalchemy.pool.Pool instance. All of the options described in the previous section <&formatting.myt:link, path="pooling_configuration"&> can be specified, as well as engine-specific options:
diff --git a/doc/build/content/metadata.myt b/doc/build/content/metadata.myt index 49ca04bb27..b0d66a4986 100644 --- a/doc/build/content/metadata.myt +++ b/doc/build/content/metadata.myt @@ -5,8 +5,7 @@The core of SQLAlchemy's query and object mapping operations is table metadata, which are Python objects that describe tables. Metadata objects can be created by explicitly naming the table and all its properties, using the Table, Column, ForeignKey, and Sequence objects imported from sqlalchemy.schema, and a database engine constructed as described in the previous section, or they can be automatically pulled from an existing database schema. First, the explicit version:
<&|formatting.myt:code&> from sqlalchemy.schema import * - import sqlalchemy.engine as engine - engine = engine.create_engine('sqlite', ':memory:', {}, **opts) + engine = create_engine('sqlite', {'filename':':memory:'}, **opts) users = Table('users', engine, Column('user_id', Integer, primary_key = True), @@ -139,8 +138,8 @@ DROP TABLE employees <&|formatting.myt:code&> # create two engines - sqlite_engine = engine.create_engine('sqlite', 'querytest.db', {}) - postgres_engine = engine.create_engine('postgres', + sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'}) + postgres_engine = create_engine('postgres', {'database':'test', 'host':'127.0.0.1', 'user':'scott', 'password':'tiger'}) @@ -161,7 +160,7 @@ DROP TABLE employees Column('user_name', String(50)) ) - sqlite_engine = engine.create_engine('sqlite', 'querytest.db', {}) + sqlite_engine = create_engine('sqlite', {'filename':'querytest.db'}) sqlite_users = users.toengine(sqlite_engine) sqlite_users.create() &> diff --git a/doc/build/content/sqlconstruction.myt b/doc/build/content/sqlconstruction.myt index ec81e44c5f..65dc8f0134 100644 --- a/doc/build/content/sqlconstruction.myt +++ b/doc/build/content/sqlconstruction.myt @@ -11,9 +11,8 @@For this section, we will assume the following tables: <&|formatting.myt:code&> - from sqlalchemy.schema import * - import sqlalchemy.engine as engine - db = engine.create_engine('sqlite', 'mydb', {}, echo=True) + from sqlalchemy import * + db = create_engine('sqlite', {'filename':'mydb'}, echo=True) # a table to store users users = Table('users', db, @@ -50,7 +49,7 @@ <&|doclib.myt:item, name="select", description="Simple Select" &>
A select is done by constructing a Select object with the proper arguments, adding any extra arguments if desired, then calling its execute() method. <&|formatting.myt:code&> - from sqlalchemy.sql import * + from sqlalchemy import * # use the select() function defined in the sql package s = select([users]) -- 2.47.2