Foreign Keys#

Following Foreign Keys#

The osu! api often returns models which contain an id which references another model. For instance, Beatmap.beatmapset_id references the id of the Beatmapset model. This is a similar concept to foreign keys in databases.

Where applicable, ossapi provides methods to “follow” these ids and retrieve the full model from the id:

beatmap = api.beatmap(221777)
bmset = beatmap.beatmapset()

You can do the same for user() and beatmap(), in applicable models:

disc = api.beatmapset_discussion_posts(2641058).posts[0]
user = disc.user()

bm_playcount = api.user_beatmaps(user_id=12092800, type="most_played")[0]
beatmap = bm_playcount.beatmap()

Note

Following a foreign key usually involves an api call, so it is not free.

Other Foreign Keys#

Note that the id attribute and corresponding method isn’t always called beatmap, beatmapset, or user. For instance, BeatmapsetDiscussionPost has the attributes last_editor_id and deleted_by_id, referencing the users who last edited and deleted the discussion respectively.

In line with this, BeatmapsetDiscussionPost defines the methods last_editor and deleted_by to retrieve the full user objects:

disc = api.beatmapset_discussion_posts(2641058).posts[0]
last_editor = disc.last_editor()
deleted_by = disc.deleted_by()
print(last_editor.username, deleted_by)

Models with similar attributes also define similar methods. The functions are almost always named by dropping _id from the attribute name.