我正在尝试使用Flask在Python中开发一个基本的管理系统,并面临与autoincrement
参数有关的问题。
系统基本上包含3个表(模型),分别命名如下:
上述模型之间还存在以下两个关系:
但是,在Actor表和Donor表中,表列id
不是主键!而且,我希望他们autoincrement
而不将值显式传递给这些列。
我一直使用的database
是 MySQL ,由于我想使事情保持简单,所以我不想切换到其他数据库
这是model.py
文件中的代码,我试图在其中实现所需的功能,但该代码似乎无法正常工作。
#old import: from dms_main import db
#due to cyclic imports concept, dms_main has been replaced by __main__
from dms_main import db, login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_actor(id):
return Actor.query.get(int(id))
class Actor(db.Model, UserMixin):
__tablename__ = 'actor'
"""
The Actor class defines the model to be stored into database
It contains the fields which would be columns into database
"""
id = db.Column(db.Integer, unique = True, nullable = False, autoincrement = True)
first_name = db.Column(db.String(20), unique = False, nullable = False)
last_name = db.Column(db.String(20), unique = False, nullable = False)
email_address = db.Column(db.String(50), primary_key = True)
password = db.Column(db.String(16), unique = False, nullable = False) #dt = String of size 16 because the password will be stored in a hased format of 16 chars
#The "donors" is a relationship type and not a column. It refers to the Donor table.
#What it means is it would create an additional virtual column "actor" in the Donor table which will store the details of the actor which created that particular donor
#This relationship is not displayed in MySQL tables. The relationship is true only for SQLAlchemy
donors = db.relationship('Donor', backref = 'actor',lazy = True)
#Here, db.Model is we inherit the Model class from Python and build our model on top of it
class Donor(db.Model):
__tablename__ = 'donor'
'''
The Donor class defines the model to be stored into database
It contains the fields which would be columns into database
'''
#id = db.Column(db.Integer, unique = True, autoincrement = True)
first_name = db.Column(db.String(20), unique = False, nullable = False)
last_name = db.Column(db.String(20), unique = False, nullable = False)
email_address = db.Column(db.String(50), unique = True, nullable = False)
reference = db.Column(db.String(50), unique = False, nullable = True)
phone_number = db.Column(db.String(10), unique = True, nullable = False)
date_of_birth = db.Column(db.DATE, unique = False, nullable = False)
date_of_anniversary = db.Column(db.DATE, unique = False, nullable = True)
pan = db.Column(db.String(10), unique = True, nullable = False, primary_key = True)
aadhar = db.Column(db.String(12), unique = True, nullable = False)
address = db.Column(db.Text, unique = False, nullable = False)
city = db.Column(db.String(20), unique = False, nullable = False)
state = db.Column(db.String(25), unique = False, nullable = False)
country = db.Column(db.String(20), unique = False, nullable = False)
pin_code = db.Column(db.String(10), unique = False, nullable = False)
created_by_name = db.Column(db.String(40), unique = False, nullable = False)
created_by_email_address = db.Column(db.String(50), db.ForeignKey('actor.email_address'), nullable = False, unique = False)
created_date_time = db.Column(db.DateTime, unique = False, nullable = False)
#Concept of relationship and backref
donations = db.relationship('Donation', backref = 'donor', lazy = True)
#Each class has one constructor, similar to java
#def __init__(self, id, first_name, ...):
# self.id = id;
# self.first_name = first_name;
# .....
#But since we inherit the class Model, we need not write the constructor!!
class Donation(db.Model):
'''
The donation class defines the model to be stored into the database
It will be used to store the details of the donations made into the system
'''
id = db.Column(db.Integer, autoincrement = True, primary_key = True)
timestamp = db.Column(db.DateTime, unique = False, nullable = False)
donation_type_cash = db.Column(db.Boolean, default = True, nullable = True)
donation_type_cheque = db.Column(db.Boolean, default = False, nullable = True)
donation_type_kind = db.Column(db.Boolean, default = False, nullable = True)
pan = db.Column(db.String(10), db.ForeignKey('donor.pan'), unique = False, nullable = False)
project = db.Column(db.String(20), unique = False, nullable = True)
我提到了与同一问题相关的其他类似问题的解决方案,但找不到可行的逻辑
有人会建议我摆脱困境吗?