librelist archives

« back to archive

Database initialization

Database initialization

From:
Maël Primet
Date:
2012-08-06 @ 16:35
Here is my setup:

    + server/
    |- __init__.py
    |- models.py
    |- routes.py
    run_server.py
    create_database.py

__init__.py:
    app = Flask(...)
    ...
    db = SQLAlchemy(app)
    ...
    import routes

models.py:
    from server import db
    User = ...

routes.py:
    import server.models
    anonymous_user = User.get_by_name('anon') # cache the anon user
    ...
    @app.route(...)

create_database.py:
    from server import db
    import server.models
    db.drop_all()
    db.create_all()
    db.add(User('anon'))
    db.commit()

I want to cache the anonymous user in my routes to serve it sometimes, and
I also want to have a script to fill database tables before I run the app

my problem is this:

if no table is created, routes.py will fail when I run User.get_by_name (it
will try to fetch it in a table), however:
 - to create the table I need to load server
 - server loads the routes
 - the routes will not load because the table does not exist

so if I want this to work, I have to modify __init__.py by doing this:

__init__.py:
    db = SQLAlchemy(app)
    import models
    db.create_all()
    ...

it doesn't feel right, is there a more starightfoward way to be able:
 - to have a script to fill database tables
 - to share the models between server and script
 - to lookup the database when loading the server
 - without cycles? (eg I need a database with table to launch my server,
but I need to launch my server to create tables )

Re: [flask] Database initialization

From:
Lyndsy Simon
Date:
2012-08-06 @ 16:50
Why can't you execute database.py from the command line?

I don't understand your project structure - what does "./server/" contain?
- but you do not have to have an active request context in order to execute
the code in database.py.

Thanks,
Lyndsy Simon



On Mon, Aug 6, 2012 at 11:35 AM, Maël Primet <mael.primet@gmail.com> wrote:

> Here is my setup:
>
>     + server/
>     |- __init__.py
>     |- models.py
>     |- routes.py
>     run_server.py
>     create_database.py
>
> __init__.py:
>     app = Flask(...)
>     ...
>     db = SQLAlchemy(app)
>     ...
>     import routes
>
> models.py:
>     from server import db
>     User = ...
>
> routes.py:
>     import server.models
>     anonymous_user = User.get_by_name('anon') # cache the anon user
>     ...
>     @app.route(...)
>
> create_database.py:
>     from server import db
>     import server.models
>     db.drop_all()
>     db.create_all()
>     db.add(User('anon'))
>     db.commit()
>
> I want to cache the anonymous user in my routes to serve it sometimes, and
> I also want to have a script to fill database tables before I run the app
>
> my problem is this:
>
> if no table is created, routes.py will fail when I run User.get_by_name
> (it will try to fetch it in a table), however:
>  - to create the table I need to load server
>  - server loads the routes
>  - the routes will not load because the table does not exist
>
> so if I want this to work, I have to modify __init__.py by doing this:
>
> __init__.py:
>     db = SQLAlchemy(app)
>     import models
>     db.create_all()
>     ...
>
> it doesn't feel right, is there a more starightfoward way to be able:
>  - to have a script to fill database tables
>  - to share the models between server and script
>  - to lookup the database when loading the server
>  - without cycles? (eg I need a database with table to launch my server,
> but I need to launch my server to create tables )
>
>

Re: [flask] Database initialization

From:
Maël Primet
Date:
2012-08-06 @ 17:16
Hi Lindsky:

sorry, __init__.py, models.py and routes.py are in the server directory
and run_server.py and create_database.py are outside (they launch the app
or fill the database)

I cannot launch directly create_database.py when no table has been built,
because creating databases involves loading the models from the server, but
importing server.models also imports server, which contains my app, and my
app expects some stuff to be already in the database, so it launches some
exception because of the cycle ;

is there a  more  elegant way to do it that you know?

On Mon, Aug 6, 2012 at 6:50 PM, Lyndsy Simon <lyndsy@lyndsysimon.com> wrote:

> Why can't you execute database.py from the command line?
>
> I don't understand your project structure - what does "./server/" contain?
> - but you do not have to have an active request context in order to execute
> the code in database.py.
>
> Thanks,
> Lyndsy Simon
>
>
>
> On Mon, Aug 6, 2012 at 11:35 AM, Maël Primet <mael.primet@gmail.com>wrote:
>
>> Here is my setup:
>>
>>     + server/
>>     |- __init__.py
>>     |- models.py
>>     |- routes.py
>>     run_server.py
>>     create_database.py
>>
>> __init__.py:
>>     app = Flask(...)
>>     ...
>>     db = SQLAlchemy(app)
>>     ...
>>     import routes
>>
>> models.py:
>>     from server import db
>>     User = ...
>>
>> routes.py:
>>     import server.models
>>     anonymous_user = User.get_by_name('anon') # cache the anon user
>>     ...
>>     @app.route(...)
>>
>> create_database.py:
>>     from server import db
>>     import server.models
>>     db.drop_all()
>>     db.create_all()
>>     db.add(User('anon'))
>>     db.commit()
>>
>> I want to cache the anonymous user in my routes to serve it sometimes,
>> and I also want to have a script to fill database tables before I run the
>> app
>>
>> my problem is this:
>>
>> if no table is created, routes.py will fail when I run User.get_by_name
>> (it will try to fetch it in a table), however:
>>  - to create the table I need to load server
>>  - server loads the routes
>>  - the routes will not load because the table does not exist
>>
>> so if I want this to work, I have to modify __init__.py by doing this:
>>
>> __init__.py:
>>     db = SQLAlchemy(app)
>>     import models
>>     db.create_all()
>>     ...
>>
>> it doesn't feel right, is there a more starightfoward way to be able:
>>  - to have a script to fill database tables
>>  - to share the models between server and script
>>  - to lookup the database when loading the server
>>  - without cycles? (eg I need a database with table to launch my server,
>> but I need to launch my server to create tables )
>>
>>
>

Re: [flask] Database initialization

From:
Shriram Kunchanapalli
Date:
2012-08-09 @ 07:22
If create database can happen if table's available , will from server
import models not work ?
It looks like for create database to work your routes should be isolated
from it or
May be add try except for querying User table to avoid exception in routes
and after database is created successfully, user table query in routes.py
should work

Can you briefly explain the initial sequence of steps you expect the app to
do  ? It might help restructure or redesign if appropriate

On Monday, August 6, 2012, Maël Primet wrote:

> Hi Lindsky:
>
> sorry, __init__.py, models.py and routes.py are in the server directory
> and run_server.py and create_database.py are outside (they launch the app
> or fill the database)
>
> I cannot launch directly create_database.py when no table has been built,
> because creating databases involves loading the models from the server, but
> importing server.models also imports server, which contains my app, and my
> app expects some stuff to be already in the database, so it launches some
> exception because of the cycle ;
>
> is there a  more  elegant way to do it that you know?
>
> On Mon, Aug 6, 2012 at 6:50 PM, Lyndsy Simon 
<lyndsy@lyndsysimon.com<javascript:_e({}, 'cvml', 
'lyndsy@lyndsysimon.com');>
> > wrote:
>
>> Why can't you execute database.py from the command line?
>>
>> I don't understand your project structure - what does "./server/"
>> contain? - but you do not have to have an active request context in order
>> to execute the code in database.py.
>>
>> Thanks,
>> Lyndsy Simon
>>
>>
>>
>> On Mon, Aug 6, 2012 at 11:35 AM, Maël Primet 
<mael.primet@gmail.com<javascript:_e({}, 'cvml', 
'mael.primet@gmail.com');>
>> > wrote:
>>
>>> Here is my setup:
>>>
>>>     + server/
>>>     |- __init__.py
>>>     |- models.py
>>>     |- routes.py
>>>     run_server.py
>>>     create_database.py
>>>
>>> __init__.py:
>>>     app = Flask(...)
>>>     ...
>>>     db = SQLAlchemy(app)
>>>     ...
>>>     import routes
>>>
>>> models.py:
>>>     from server import db
>>>     User = ...
>>>
>>> routes.py:
>>>     import server.models
>>>     anonymous_user = User.get_by_name('anon') # cache the anon user
>>>     ...
>>>     @app.route(...)
>>>
>>> create_database.py:
>>>     from server import db
>>>     import server.models
>>>     db.drop_all()
>>>     db.create_all()
>>>     db.add(User('anon'))
>>>     db.commit()
>>>
>>> I want to cache the anonymous user in my routes to serve it sometimes,
>>> and I also want to have a script to fill database tables before I run the
>>> app
>>>
>>> my problem is this:
>>>
>>> if no table is created, routes.py will fail when I run User.get_by_name
>>> (it will try to fetch it in a table), however:
>>>  - to create the table I need to load server
>>>  - server loads the routes
>>>  - the routes will not load because the table does not exist
>>>
>>> so if I want this to work, I have to modify __init__.py by doing this:
>>>
>>> __init__.py:
>>>     db = SQLAlchemy(app)
>>>     import models
>>>     db.create_all()
>>>     ...
>>>
>>> it doesn't feel right, is there a more starightfoward way to be able:
>>>  - to have a script to fill database tables
>>>  - to share the models between server and script
>>>  - to lookup the database when loading the server
>>>  - without cycles? (eg I need a database with table to launch my server,
>>> but I need to launch my server to create tables )
>>>
>>>
>>
>