|
20 | 20 | database = databases.Database(DATABASE_URL)
|
21 | 21 |
|
22 | 22 |
|
| 23 | +class Role(ormar.Model): |
| 24 | + class Meta: |
| 25 | + tablename = "roles" |
| 26 | + metadata = metadata |
| 27 | + database = database |
| 28 | + |
| 29 | + id = ormar.UUID(primary_key=True, uuid_format="string") |
| 30 | + name = ormar.String(nullable=False, max_length=255) |
| 31 | + |
| 32 | + |
23 | 33 | class User(OrmarBaseUserModel):
|
24 | 34 | class Meta:
|
25 | 35 | metadata = metadata
|
26 | 36 | database = database
|
27 | 37 |
|
28 | 38 | first_name = ormar.String(nullable=True, max_length=255)
|
| 39 | + roles = ormar.ManyToMany(Role, skip_reverse=True) |
29 | 40 |
|
30 | 41 |
|
31 | 42 | class OAuthAccount(OrmarBaseOAuthAccountModel):
|
@@ -196,3 +207,27 @@ async def test_queries_oauth(
|
196 | 207 | # Unknown OAuth account
|
197 | 208 | unknown_oauth_user = await ormar_user_db_oauth.get_by_oauth_account("foo", "bar")
|
198 | 209 | assert unknown_oauth_user is None
|
| 210 | + |
| 211 | + |
| 212 | +@pytest.mark.asyncio |
| 213 | +@pytest.mark.db |
| 214 | +async def test_queries_custom_fields_relations(ormar_user_db: OrmarUserDatabase[UserDB]): |
| 215 | + # Create role to pair with |
| 216 | + role = await Role.objects.create( |
| 217 | + id=uuid.uuid4(), |
| 218 | + name='editor' |
| 219 | + ) |
| 220 | + |
| 221 | + assert role.id is not None |
| 222 | + |
| 223 | + user = UserDB( |
| 224 | + email="lancelot@camelot.bt", |
| 225 | + hashed_password="guinevere", |
| 226 | + roles=[role] |
| 227 | + ) |
| 228 | + |
| 229 | + # Create with relationship |
| 230 | + user_db = await ormar_user_db.create(user) |
| 231 | + assert user_db.roles is not None |
| 232 | + assert len(user_db.roles) is not 0 |
| 233 | + assert user_db.roles[0].id == role.id |
0 commit comments