@@ -309,23 +309,49 @@ def map_constraint(((var, _expression), value)):
309
309
'Name already defined as generator: %s' %
310
310
(repr (phrase .name ),))
311
311
else :
312
+ # Insert a record into bayesdb_generator and get the
313
+ # assigned id.
312
314
bdb .sql_execute ('''
313
315
INSERT INTO bayesdb_generator
314
316
(name, tabname, population_id, metamodel)
315
317
VALUES (?, ?, ?, ?)
316
318
''' , (phrase .name , table , population_id , metamodel .name ()))
317
319
generator_id = core .bayesdb_get_generator (bdb , phrase .name )
320
+
321
+ # Populate bayesdb_generator_column.
322
+ #
318
323
# XXX Omit needless bayesdb_generator_column table --
319
324
# Github issue #441.
320
325
bdb .sql_execute ('''
321
326
INSERT INTO bayesdb_generator_column
322
327
(generator_id, colno, stattype)
323
- SELECT ? , colno, stattype
328
+ SELECT :generator_id , colno, stattype
324
329
FROM bayesdb_variable
325
- WHERE population_id = ?
326
- ''' , (generator_id , population_id ))
330
+ WHERE population_id = :population_id
331
+ AND generator_id IS NULL
332
+ ''' , {
333
+ 'generator_id' : generator_id ,
334
+ 'population_id' : population_id ,
335
+ })
336
+
337
+ # Do any metamodel-specific initialization.
327
338
metamodel .create_generator (bdb , generator_id , phrase .schema )
328
339
340
+ # Populate bayesdb_generator_column with any latent
341
+ # variables that metamodel.create_generator has added
342
+ # with bayesdb_add_latent.
343
+ bdb .sql_execute ('''
344
+ INSERT INTO bayesdb_generator_column
345
+ (generator_id, colno, stattype)
346
+ SELECT :generator_id, colno, stattype
347
+ FROM bayesdb_variable
348
+ WHERE population_id = :population_id
349
+ AND generator_id = :generator_id
350
+ ''' , {
351
+ 'generator_id' : generator_id ,
352
+ 'population_id' : population_id ,
353
+ })
354
+
329
355
# All done. Nothing to return.
330
356
return empty_cursor (bdb )
331
357
0 commit comments