SQLAlchemy:sqlalchemy.exc.CompileError无法为NullType()生成DDL

时间:2018-07-25 07:36:51

标签: python flask-sqlalchemy

我使用 python3.7 \ mysql5.7 \ flask:0.12.2 \ Flask-SQLAlchemy:2.3.2 \ PyMySQL:0.9.2

代码:

config.py

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@localhost:3306/test?charset=utf8'

SQLALCHEMY_TRACK_MODIFICATIONS = True

demodb.py

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object("config")
db=SQLAlchemy(app)
db.create_all()
class User(db.Model):
   __tablename__='user'
   id = db.Column(db.Integer, primary_key=True)
   username = db.Column(db.String(80), unique=True)
   email = db.Column(db.String(120), unique=True)

   def __init__(self, username, email):
      self.username = username
      self.email = email

   def __repr__(self):
      return '<User %r>' % self.username
db.create_all()

错误:

sqlalchemy.exc.CompileError: (in table 'user', column 'id'): Can't generate DDL for NullType(); did you forget to specify a type on this Column?

所以,请帮我解决这个问题。3Q

********* THEN ***********

我尝试:

config.py

SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:123456@localhost:3306/testuse'
# SQLALCHEMY_TRACK_MODIFICATIONS = True

demodb.py

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object("config")
db=SQLAlchemy(app)
# db.create_all()
class User(db.Model):
   __tablename__='user'
   id = db.Column(db.Integer, primary_key=True,autoincrement=True)
   username = db.Column(db.String(80), nullable=True)
   email = db.Column(db.String(120), nullable=True)

   def __init__(self, username, email):
      self.username = username
      self.email = email

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

db.create_all()

运行

E:\myproject\env\lib\site-packages\flask_sqlalchemy\__init__.py:795: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
E:\myproject\env\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 481")
  result = self._query(query)
 * Debugger is active!
 * Debugger PIN: 579-465-691
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

因此,成功创建了表格(我的问题:有一个警告,如何解决): successfully create table

0 个答案:

没有答案