我正在遵循flask-sqlalchemy教程,宣布关于一对多关系的模型.示例代码如下:
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
addresses = db.relationship('Address', backref='person',
lazy='dynamic')
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何使用这样的模型将新记录插入到DB中.我假设我需要一个构造函数init,但我很难理解它应该如何实现和使用.我这里的主要问题是Person依赖于地址和地址有ForeignKey to Person,所以它应该提前知道Person.
Plase帮助我理解它应该如何执行.
先感谢您.
python sqlalchemy foreign-keys relationship flask-sqlalchemy
我一直在尝试安装NumPy,并且已经有了残酷的时间.无论我尝试什么,我都会收到异常错误.我用了这个命令
$pip install numpy
Run Code Online (Sandbox Code Playgroud)
但它抛出了这个错误
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/basecommand.py", line 246, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/commands/install.py", line 352, in run
root=options.root_path,
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/req/req_set.py", line 693, in install
**kwargs
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/req/req_install.py", line 817, in install
self.move_wheel_files(self.source_dir, root=root)
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/req/req_install.py", line 1018, in move_wheel_files
isolated=self.isolated,
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/wheel.py", line 269, in move_wheel_files
clobber(source, dest, False, fixer=fixer, filter=filter)
File "/Library/Python/2.7/site-packages/pip-6.1.1-py2.7.egg/pip/wheel.py", line 215, in clobber
shutil.copyfile(srcfile, destfile)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst: …Run Code Online (Sandbox Code Playgroud)