@@ -309,23 +309,49 @@ def map_constraint(((var, _expression), value)):
309309 'Name already defined as generator: %s' %
310310 (repr (phrase .name ),))
311311 else :
312+ # Insert a record into bayesdb_generator and get the
313+ # assigned id.
312314 bdb .sql_execute ('''
313315 INSERT INTO bayesdb_generator
314316 (name, tabname, population_id, metamodel)
315317 VALUES (?, ?, ?, ?)
316318 ''' , (phrase .name , table , population_id , metamodel .name ()))
317319 generator_id = core .bayesdb_get_generator (bdb , phrase .name )
320+
321+ # Populate bayesdb_generator_column.
322+ #
318323 # XXX Omit needless bayesdb_generator_column table --
319324 # Github issue #441.
320325 bdb .sql_execute ('''
321326 INSERT INTO bayesdb_generator_column
322327 (generator_id, colno, stattype)
323- SELECT ? , colno, stattype
328+ SELECT :generator_id , colno, stattype
324329 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.
327338 metamodel .create_generator (bdb , generator_id , phrase .schema )
328339
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+
329355 # All done. Nothing to return.
330356 return empty_cursor (bdb )
331357
0 commit comments