Flask-SQLAlchemy没有创建我的表 [英] Flask-SQLAlchemy not creating my tables

查看:440
本文介绍了Flask-SQLAlchemy没有创建我的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQLAlchemy,由于某种原因,当我运行 create_db.py 时,只会创建迁移表. 我从python终端使用from modules import db,models进行了尝试,然后运行 db.create_all() ,但它仍然给出相同的结果.

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.

这是我的 models.py .

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()

这是我的 create_db.py

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))

推荐答案

更改

from __init__ import db

from modules import db

在models.py中有效. 从外部程序包运行flask应用程序时,需要从程序包本身而不是单个模块中导入所有内容.

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.

这篇关于Flask-SQLAlchemy没有创建我的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆