README.md 1.33 KB
Newer Older
Gerion Entrup's avatar
Gerion Entrup committed
1
2
3
4
5
6
MusicBrainz Database Tools
==========================

SQLAlchemy Models
-----------------

Gerion Entrup's avatar
Typo    
Gerion Entrup committed
7
This is a flattened version of the MusicBrainz Database Tools from Lukáš Lalinský.
Gerion Entrup's avatar
Gerion Entrup committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
See https://bitbucket.org/lalinsky/mbdata for the original.

The code is modified to run with Python 3.

If you are developing a Python application that needs access to the MusicBrainz
data, you can use the `mbdata.models` module to get [SQLAlchemy](http://www.sqlalchemy.org/)
models mapped to the MusicBrainz database tables.

All tables from the MusicBrainz database are mapped, all foreign keys have one-way
relationships set up and some models, where it's essential to access their
related models, have two-way relationships (collections) set up.

In order to work with the relationships efficiently, you should use the appropriate
kind of [eager loading](http://docs.sqlalchemy.org/en/rel_0_8/orm/loading.html).

Example:

    #!python
    >>> from sqlalchemy import create_engine
    >>> from sqlalchemy.orm import sessionmaker
    >>> from mbdata.models import Artist
    >>> engine = create_engine('postgresql://musicbrainz:musicbrainz@127.0.0.1/musicbrainz', echo=True)
    >>> Session = sessionmaker(bind=engine)
    >>> session = Session()
    >>> artist = session.query(Artist).filter_by(gid='b10bbbfc-cf9e-42e0-be17-e2c3e1d2600d').first()
    >>> print artist.name