[ckan-dev] Creating tables for plugin models

Sean Hammond sean.hammond at okfn.org
Thu Jun 19 10:50:35 UTC 2014


> I'm writing a plugin which includes it's own model. The model is created 
> using the SqlAlchemy MetaData object from ckan.model.meta - while this 
> isn't available via the toolkit, it seemed to make more sense than 
> creating a separate SqlAlchemy engine. I import the model from plugin.py 
> to ensure that the model is added to the metadata when the plugin is 
> loaded (as this particular plugin implement IConfigurer that happens 
> early on).

This sounds right to me (and I think it's how existing plugins with
their own models do it). Sounds like a case for something that needs to
be added to the toolkit though.

> On an already setup site I was expecting running `paster --plugin=ckan 
> db upgrade -c development.ini` would create my table - but that didn't 
> work. What did work however is running `paster --plugin=ckan db 
> create-from-model -c development.ini`.
> 
> Is this how it is expected to work? Or is it better to create a custom 
> command in the plugin to create just that table?

If you look at, I think, ckanext-harvest or ckanext-spatial, I can't
remember exactly how it works but they have some code in there that
creates their database tables if they don't already exist.



More information about the ckan-dev mailing list