File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -315,6 +315,8 @@ def _init_table(cls, sub_cls):
315
315
updates [k ] = sub_cls .__attr_factory__ (k , v )
316
316
elif isinstance (v , (sa .Index , sa .Constraint )):
317
317
inspected_args .append (v )
318
+ elif isinstance (v , json_support .JSONProperty ):
319
+ updates [k ] = v
318
320
if table_name is None :
319
321
return
320
322
sub_cls ._column_name_map = column_name_map
@@ -351,6 +353,8 @@ def _init_table(cls, sub_cls):
351
353
for each_cls in sub_cls .__mro__ [::- 1 ]:
352
354
for k , v in each_cls .__dict__ .items ():
353
355
if isinstance (v , json_support .JSONProperty ):
356
+ if not v .name :
357
+ v .name = k
354
358
json_col = getattr (
355
359
sub_cls .__dict__ .get (v .prop_name ), "column" , None
356
360
)
Original file line number Diff line number Diff line change @@ -179,6 +179,34 @@ class Another(Audit, db.Model):
179
179
assert False , "Should not reach here"
180
180
181
181
182
+ async def test_mixin_crud (engine ):
183
+ db = gino .Gino ()
184
+ db .bind = engine
185
+
186
+ class Mixin :
187
+ id = db .Column (db .Integer , primary_key = True )
188
+ name = db .Column (db .Text )
189
+
190
+ class MixinCrud (db .Model , Mixin ):
191
+ __tablename__ = "mixin_crud"
192
+
193
+ await db .gino .create_all ()
194
+ try :
195
+ mc = await MixinCrud .create (name = "mctest" )
196
+ assert mc .name == "mctest"
197
+
198
+ await mc .update (name = "updated" ).apply ()
199
+
200
+ mc = await MixinCrud .query .gino .first ()
201
+ assert mc .name == "updated"
202
+
203
+ await mc .delete ()
204
+ mc = await MixinCrud .query .gino .first ()
205
+ assert mc is None
206
+ finally :
207
+ await db .gino .drop_all ()
208
+
209
+
182
210
# noinspection PyUnusedLocal
183
211
async def test_inherit_constraint ():
184
212
with pytest .raises (ValueError , match = "already attached to another table" ):
Original file line number Diff line number Diff line change @@ -312,3 +312,27 @@ def age_idx(cls):
312
312
313
313
await IndexTest .gino .create ()
314
314
await IndexTest .gino .drop ()
315
+
316
+
317
+ async def test_mixin (bind ):
318
+ from gino .dialects .asyncpg import JSONB
319
+
320
+ class Base :
321
+ id = db .Column (db .Integer (), primary_key = True )
322
+ profile = db .Column (JSONB ())
323
+
324
+ class Mixin (Base ):
325
+ age = db .IntegerProperty ()
326
+
327
+ class MixinTest (db .Model , Mixin ):
328
+ __tablename__ = "mixin_test"
329
+
330
+ await MixinTest .gino .create ()
331
+ try :
332
+ mt = await MixinTest .create (age = 22 )
333
+ await mt .update (age = 24 ).apply ()
334
+ assert (await MixinTest .query .gino .first ()).age == 24
335
+ await mt .update (age = MixinTest .age - 5 ).apply ()
336
+ assert (await MixinTest .query .gino .first ()).age == 19
337
+ finally :
338
+ await MixinTest .gino .drop ()
You can’t perform that action at this time.
0 commit comments