[odb-users] Separate database schema. "Unknown database" error

Boris Kolpackov boris at codesynthesis.com
Thu Oct 3 08:44:18 EDT 2013


Hi Роман,

Роман Куксин <rkuksin91 at mail.ru> writes:

> I'm trying to create several sqlite files with different database
> schemas.
>
> [...]
>
> #pragma db object schema("one")
> class one
>
> [...]
>
> schema_catalog::create_schema (*db, schemaName);
>
> [...]
>
> --schema-name one

Unfortunately, the term 'schema' means two different things in the
relational database world. A schema can mean the database structure,
that is tables, columns, etc., and when we say "create schema" we
mean create the tables, etc.

In some databases, such as Oracle, tables, etc., can be placed into
separate namespaces and then we could qualify a table with such a
namespace, for example, foo.bar. Unfortunately, such namespaces are
also called schema.

While some databases don't support schemas (namespaces), they still
allow us to qualify table names with some other names. For example,
in SQLite, we can attach another database file to an existing 
connection, give it a name, and then use that name to refer to
tables in this file.

As a result, in ODB, any kind of table qualification is called
'schema'. What it means exactly is databale dependant. A schema
can be assigned either with the schema pragma (as you did in
your case) or with the --schema option.

When the database schema (i.e., the database structure) is embedded
into the generated C++ code, it can sometimes be useful to partition
it into multiple segments so that we can create databases only with
a certain set of tables, etc. This is exactly what you are trying
to achieve.

To support this, ODB allows us to assign names to schemas on the
per-file basis using the --schema-name option. In other words,
if you compile foo.hxx and pass --schema-name 'foo', then the
schema for all the persistent classes in this file (and any
other file compiled with the same option) will be called "foo".
Note the subtle difference between option names: --schema and
--schema-name.

I think now it should be clear what you need to do:

1. Split your header file into two files each containing one class.

2. Remove the schema pragma. You will only need it if you want to
   attach both files to the same database instance.

3. Compile the two files and assing each a different schema name:

   odb ... --schema-name one one.hxx 
   odb ... --schema-name two two.hxx 

4. Pass the schema name to create_database:

   auto db1 = create_database("1.db", "one");
   auto db2 = create_database("2.db", "two");

Boris



More information about the odb-users mailing list