Flask-SQLAlchemy 没有创建我的表
Flask-SQLAlchemy not creating my tables
SQLAlchemy 并且出于某种原因,当我运行我的 create_db.py
时,只创建了迁移 table。
我从 python 终端用 from modules import db,models
尝试了它,然后运行 db.create_all()
但它仍然给出相同的结果。
这是我的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
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 应用程序需要从包本身而不是单个模块导入所有内容时。
SQLAlchemy 并且出于某种原因,当我运行我的 create_db.py
时,只创建了迁移 table。
我从 python 终端用 from modules import db,models
尝试了它,然后运行 db.create_all()
但它仍然给出相同的结果。
这是我的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
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 应用程序需要从包本身而不是单个模块导入所有内容时。