在Flask-sqlalchemy中创建后无法看到表格

时间:2017-10-25 19:23:27

标签: python mysql sqlalchemy

我正在尝试SQL炼金术和烧瓶。我正在尝试创建一个表,其中我与另一个表有外键关系。下面是application.py文件的完整代码:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_pyfile('config.cfg')

db = SQLAlchemy(app)

class Member(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(20),unique=True)
    password = db.Column(db.String(30))
    email = db.Column(db.String(30))
    join_date = db.Column(db.DateTime)

    orders = db.relationship('Order',lazy='dynamic')

    def __repr__(self):
        return '<Member %r>' % self.username

class Order(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    price = db.Column(db.Integer)
    member_id = db.Column(db.Integer,db.ForeignKey('member.id'))

    member = db.relationship('Member')

if __name__ == "__main__":
    app.run()

现在导入这个文件和python REPL中的类后我运行了这个: db.create_all()

我可以在MYSQL中看到这些表。

mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| member           |
| order            |
+------------------+
2 rows in set (0.01 sec)

我甚至可以在第一张表(会员)上做一个描述:

mysql> desc member;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| username  | varchar(20) | YES  | UNI | NULL    |                |
| password  | varchar(30) | YES  |     | NULL    |                |
| email     | varchar(30) | YES  |     | NULL    |                |
| join_date | datetime    | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

但是当我尝试对表顺序执行相同操作时,它会给我一个错误:

mysql> desc order;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near 'ord
er' at line 1

有人可以指出问题究竟在哪里吗?

提前致谢。

0 个答案:

没有答案