Rows

Use of this feature ensures the existence of rows matching the defined values. All columns included in the primary key (as defined by the MetaData) are required to be included on a given Row.

Using uniqueness given by the primary key commands will be emitted to bring the state of the world to match the defined values. This includes:

  • Inserting rows if none exist

  • (Optionally) Deleting them if they’re not defined but exist in the database

  • Updating them if the state of the database does not match the defined values.

    Note with updates, only the set of included fields will be updated. A given Row that excludes (non-nullable, say) columns will not be considered when performing the comparison against existing data.

from sqlalchemy_declarative_extensions import Row, Rows

# Assumes some model/table defined for `foo`.
rows = Rows().are(
    Row("foo", id=2, name="asdf"),
    Row("foo", id=3, name="qwer", active=False),
)

Note one cannot usually use the models themselves to define row data, because usually models are defined off the declarative base class. This would cause issues of circular reliance on the definition of the declarative base being complete.

If defined using alternative SQLAlchemy APIs for model definition, one can technically get around the circularity issues; so it’s likely that a future release will add support for subbing in models themselves in place of a Row.

class sqlalchemy_declarative_extensions.row.base.Row(tablename, *, schema=None, **column_values)
Parameters:

schema (str | None)

column_values: dict[str, Any]
property qualified_name
qualify(schema)

Attach a schema to this row, if it doesn’t have one already.

Examples

>>> row = Row("foo")
>>> row.qualify("bar")
Row(schema='bar', tablename='foo', column_values={})
Parameters:

schema (str | None)

Return type:

Row

schema: str | None
tablename: str
class sqlalchemy_declarative_extensions.row.base.Rows
are(*rows)
Parameters:

rows (Row)

classmethod coerce_from_unknown(unknown)
Parameters:

unknown (None | Iterable[Row] | Rows)

Return type:

Rows | None

classmethod extract(metadata)
Parameters:

metadata (sqlalchemy.MetaData | list[sqlalchemy.MetaData | None] | None)

Return type:

tuple[Self, sqlalchemy.MetaData] | None

ignore_unspecified: bool = False
included_tables: list[str] = []
rows: list[Row] = []
class sqlalchemy_declarative_extensions.row.base.Table(name, **column_values)

Convenience class for producing multiple rows against the same table.

Examples

Rows might be created directly, like so:

>>> [
...     Row("users", id=1, name="John", active=True),
...     Row("users", id=2, name="Bob", active=True),
... ]
[Row(schema=None, tablename='users', column_values={'id': 1, 'name': 'John', 'active': True}), Row(schema=None, tablename='users', column_values={'id': 2, 'name': 'Bob', 'active': True})]

But use of Table can help elide repetition among those rows:

>>> users = Table("users", active=True)
>>> [
...     users.row(id=1, name="John"),
...     users.row(id=2, name="Bob"),
... ]
[Row(schema=None, tablename='users', column_values={'active': True, 'id': 1, 'name': 'John'}), Row(schema=None, tablename='users', column_values={'active': True, 'id': 2, 'name': 'Bob'})]
column_values: dict[str, Any]
name: str
row(**column_values)
Return type:

Row

sqlalchemy_declarative_extensions.row.base.split_schema(tablename, *, schema=None)
Parameters:
  • tablename (str)

  • schema (str | None)

Return type:

tuple[str | None, str]