8

I have a create_all call in my SQLAlchemy app.py

@app.before_first_request
def create_tables():
    db.create_all()

And define a basic user in a user.py model file:

class UserModel(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(80))
    password = db.Column(db.String(80))

    def __init__(self, username, password):
        self.username = username
        self.password = password

I want to pre-populate some default data, in this example some base users:

    admin = self('admin', 'test')
    guest = self('guest', 'test')

Can I input them in the app.py somehow? Or in another create_data.py type file?

1

2 Answers 2

9

You could create an example_data.py file like this:

from user import UserModel

def db_load_example_data(app, db):
    admin = UserModel('admin', 'test')
    guest = UserModel('guest', 'test')
    with app.app_context():
        db.session.add(admin)
        db.session.add(guest)
        db.commit()

and then in your startup script you can call this

from example_data import db_load_example_data

db_load_example_data(app, db)
Sign up to request clarification or add additional context in comments.

Comments

0

Populate the table in an after_create event handler. The code will execute after the table has been created by db.create_all(). In this example I have inserted the data directly into the model's __table__, rather than using a session to create UserModel instances. This is so that the data is inserted in the same transaction as that which creates the table. Using a session (db.session.add(Usermodel(..)) seems to work, but uses a different connection which could potentially cause problems.

#Place this code after the model definition.

@db.event.listens_for(UserModel.__table__, 'after_create')                                                                                                                                                                                                                    
def initialise_values(target, connection, **kw):   
    # target is the newly created "users" table.                                                                                                                                                                                                                           
    ins = target.insert()                                                                                                                                                                                                                                                     
    values = [                                                                                                                                                                                                                                                                
        {'username': 'admin', 'password': 'admin'},                                                                                                                                                                                                                           
        {'username': 'guest', 'password': 'guest'},                                                                                                                                                                                                                           
    ]                                                                                                                                                                                                                                                                         
    connection.execute(ins, values)  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.