Schema¶
Schema and VirtualModule classes
Schema management for DataJoint.
This module provides the Schema class for binding Python table classes to database schemas, and utilities for schema introspection and management.
ordered_dir ¶
ordered_dir(class_)
List class attributes respecting declaration order.
Similar to the dir() built-in, but preserves attribute declaration
order as much as possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_
|
type
|
Class to list members for. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Attributes declared in class_ and its superclasses. |
VirtualModule ¶
Bases: ModuleType
A virtual module representing a DataJoint schema from database tables.
Creates a Python module with table classes automatically generated from the database schema. Useful for accessing schemas without Python source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module_name
|
str
|
Display name for the module. |
required |
schema_name
|
str
|
Database schema name. |
required |
create_schema
|
bool
|
If True, create the schema if it doesn't exist. Default False. |
False
|
create_tables
|
bool
|
If True, allow declaring new tables. Default False. |
False
|
connection
|
Connection
|
Database connection. Defaults to |
None
|
add_objects
|
dict
|
Additional objects to add to the module namespace. |
None
|
Examples:
>>> lab = dj.VirtualModule('lab', 'my_lab_schema')
>>> lab.Subject.fetch()
list_schemas ¶
list_schemas(connection=None)
List all accessible schemas on the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection
|
Connection
|
Database connection. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Names of all accessible schemas. |
virtual_schema ¶
virtual_schema(schema_name, *, connection=None, create_schema=False, create_tables=False, add_objects=None)
Create a virtual module for an existing database schema.
This is the recommended way to access database schemas when you don't have the Python source code that defined them. Returns a module-like object with table classes as attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_name
|
str
|
Database schema name. |
required |
connection
|
Connection
|
Database connection. Defaults to |
None
|
create_schema
|
bool
|
If True, create the schema if it doesn't exist. Default False. |
False
|
create_tables
|
bool
|
If True, allow declaring new tables. Default False. |
False
|
add_objects
|
dict
|
Additional objects to add to the module namespace. |
None
|
Returns:
| Type | Description |
|---|---|
VirtualModule
|
A module-like object with table classes as attributes. |
Examples:
>>> lab = dj.virtual_schema('my_lab')
>>> lab.Subject.fetch()
>>> lab.Session & "subject_id='M001'"
See Also
Schema : For defining new schemas with Python classes. VirtualModule : The underlying class (prefer virtual_schema function).