0

SQLAlchemy and for some reason when i run my create_db.py only the migration table is created. I tried it from python terminal with from modules import db,models then running db.create_all() but it still gives the same result.

this is my models.py.

from __init__ import db
from datetime import datetime

class Batch(db.Model):
    __tablename__='batch'
    batch_id = db.Column(db.String, primary_key=True)
    #total = db.Column(db.Integer)
    success = db.Column(db.Integer)
    failure = db.Column(db.Integer)
    folder = db.Column(db.String(15))
    email = db.Column(db.String(20))
    detail = db.relationship('Conversion', backref='details',lazy='dynamic')
    platform = db.relationship('Platform', backref='pub_data', lazy = 'dynamic')

    def __init__(self,batch_id,success,failure,folder,email):
        self.batch_id = batch_id
        self.success = success
        self.failure = failure
        self.folder = folder
        self.email = email

class Conversion(db.Model):
    __tablename__ = 'conversion'
    id = db.Column(db.Integer, primary_key=True)
    batch_id = db.Column(db.String,db.ForeignKey('batch.batch_id'))
    file_names = db.Column(db.String)
    status = db.Column(db.String(6))
    error = db.Column(db.Text)
    res_prop = db.Column(db.Integer)        

    def __init__(self,batch_id,file_names,status,res_prop,error=None):
        self.batch_id = batch_id
        self.file_names = file_names
        self.status = status
        self.error = error
        self.res_prop = res_prop

class Platform(db.Model):
    __tablename__ = 'platform'
    id= db.Column(db.Integer,primary_key=True)
    batch_id = db.Column(db.String, db.ForeignKey('batch.batch_id'))
    title = db.Column(db.String)
    pub_date = db.Column(db.DateTime)

    def __init__(self,batch_id,title):
        self.batch_id = batch_id
        self.title = title
        self.pub_date = datetime()

And here is my create_db.py

from modules import models
from modules import db
from migrate.versioning import api
from modules.default_config import SQLALCHEMY_DATABASE_URI , SQLALCHEMY_MIGRATE_REPO
import os.path

db.create_all()
db.session.commit()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
    api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
4
  • There is no files in the versions directory? You got any errors? Commented Jan 3, 2015 at 10:31
  • @OrDuan No errors. the versions directory contains only init.py no other files. Though other migration files are created when i run migrate.py Commented Jan 3, 2015 at 10:36
  • try to import you models individually(from modules import models.Batch - or somthing like that) Commented Jan 4, 2015 at 10:52
  • I tried that too but did not work. The issue is resolved now i had to import app from modules instead of __init__ in models.py Commented Jan 4, 2015 at 12:24

1 Answer 1

2

on changing

from __init__ import db

to

from modules import db

in models.py it worked. when running flask application from outside package one needs to import everything from the package itself and not the individual modules.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.