我有 2 个模型类如下:
class Domain(db.Model):
__tablename__ = 'domain'
id = db.Column(db.Integer, primary_key=True)
domain_name = db.Column(db.String(30), unique=True)
mailboxes = db.Column(db.Integer, default=0)
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
return '%s' % self.domain_name
class EmailAccount(db.Model):
__tablename__ = 'email_account'
__table_args__ = (
db.UniqueConstraint('username', 'domain_id',
name='_uq_username_domain'),{}
)
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(30))
domain_id = db.Column(db.Integer, db.ForeignKey('domain.id'))
domain = db.relationship('Domain', backref=db.backref('emailaccounts',
lazy='dynamic'))
def __init__(self,**kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
return '%s@%s ' % (self.username, self.domain)
我只在示例中添加了此处所需的相关属性。我希望通过读取数据的 csv 文件来使用脚本填充模型。域表脚本使用 Flask-SQLAlchemy 运行良好,但 emailaccount 表脚本抛出异常。脚本如下:
#Populate domains from csv
domain_file = "domain.csv"
csv_file = csv.DictReader(open(domain_file, 'rb'), delimiter=',')
for row in csv_file:
#data type conversion from (csv)string before inserting to table
for key, value in row.items():
#some code omitted
print key, value
domain = Domain(**row)
db.session.add(domain)
db.session.commit()
#Populate accounts from csv
accounts_file = "accounts.csv"
csv_file = csv.DictReader(open(accounts_file, 'rb'), delimiter=',')
for row in csv_file:
mdomain_name = ''
#data type conversion from (csv)string before inserting to table
for key, value in row.items():
print key, value
if key == 'domain':
mdomain = Domain.query.filter_by(domain_name = value).first()
mdomain_name = mdomain.domain_name
mdomain_id = mdomain.id
if key == 'domain_id':
value = mdomain_id
account = EmailAccount(**row)
db.session.add(account)
db.session.commit()
抛出的异常是:
文件“data.py”,第 55 行,在 db.session.add(account)
文件“.../local/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py”,第 149 行,在确实返回 getattr(self.registry(), name)(*args, **kwargs)
文件“.../local/lib/python2.7/site-packages/sqlalchemy/orm/session.py”,第 1397 行,在添加 self._save_or_update_state(state)
文件“.../local/lib/python2.7/site-packages/sqlalchemy/orm/session.py”,第 1415 行,在 _save_or_update_state halt_on=self._contains_state):
文件“。 ../local/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py”,第 1986 行,在 cascade_iterator parent_dict、visited_states、halt_on))
文件“.../local/lib/python2.7/ site-packages/sqlalchemy/orm/properties.py",第 930 行,在 cascade_iterator get_all_pending(state,dict_)
文件“.../local/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py”,第 761 行,在 get_all_pending ret = [(instance_state(current), current)] AttributeError: 'str' object没有属性“_sa_instance_state”
PL。恢复 data.py 脚本中代码的更改,即为具有域类外键的 EmailAccount 模型上传数据的脚本。我希望只使用 Flask-SQLAlchemy。
account.csv 文件的提取:
Email Account,legacy_username,password,full_name,quota,is_domain_admin,is_catch_all,disabled_login,disabled_delivery
info@abc.com,,,,104857600,,,,
internal@abc.com,,,Internal,102400000,,,,
kiran.rs@abc.com,,,,102400000,,,, kishorepr,xyz.com,,,,209715200,,,,