我的关系表插入表有什么问题?

时间:2019-07-15 07:56:43

标签: python flask flask-sqlalchemy flask-migrate

我正在设置表格之间的关系,所有这些都有效,没有角色和用户表

我已经尝试过primaryjoin(),但这无济于事 当我尝试迁移时,错误提示

  

sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1091 (42000): Can't DROP 'email'; check that column/key exists.

代码:

class users(db.Model):
   id=db.Column(db.Integer,primary_key=True)
   username=db.Column(db.String(100),unique=True, index=True)
   email=db.Column(db.String(100),unique=True)
   password=db.Column(db.String(100))
   posts=db.relationship('posts',backref='user')
   comment=db.relationship('comment',backref='user_com')
   picture=db.Column(db.String(1000))
   role_id = db.Column(db.Integer,db.ForeignKey('roles.id'))


   def __init__(self,*args, **kwargs):
       super(users,self).__init__(*args,**kwargs)

   def __repr__(self):
       return 
        str({"id":self.id,"username":self.username,"email":self.email})


class roles(db.Model):
   id=db.Column(db.Integer,primary_key=True)
   name=db.Column(db.String(50),unique=True)
   users = db.relationship('users', backref='role',primaryjoin="roles.id 
         == users.role_id")

   def __init3__(self,*args,**kwargs):
    super(roles,self).__init3__(*args,**kwargs)

   def __repr3__(self):
        return'<roles id:{},name:{} >'.format(self.id,self.name)

 class posts(db.Model):

    id=db.Column(db.Integer,primary_key=True)
    title=db.Column(db.String(100))
    content=db.Column(db.Text())
    image=db.Column(db.String(100))
    user_id=db.Column(db.Integer,db.ForeignKey('users.id'))
    comment=db.relationship('comment',backref='post_com')

    def __init1__(self,*args,**kwargs):
        super(posts,self).__init1__(*args,**kwargs)

    def __repr__(self):
        return'<posts id:{},title:{} >'.format(self.id,self.title)


 class comment(db.Model):
    id=db.Column(db.Integer,primary_key=True)
    text=db.Column(db.String(200))
    post_id=db.Column(db.Integer,db.ForeignKey('posts.id'))
    user_id=db.Column(db.Integer,db.ForeignKey('users.id'))

    def __init2__(self,*args, **kwargs):
        super(comment,self).__init2__(*args,**kwargs)

    def __repr__(self):

        return str({"id":self.id,"text":self.text})

为了进行迁移,我将其写入cmd

    python manage.py db init
    python manage.py db migrate
    python manage.py db upgrade

这是完整的错误

   sqlalchemy.exc.ProgrammingError: 
   (mysql.connector.errors.ProgrammingError) 1091 (42000): Can't DROP 
   'email'; check that column/key exists
    [SQL:
     DROP INDEX email ON users]
      (Background on this error at: http://sqlalche.me/e/f405))

这是manage.py

from app import manager
from main import *
if __name__== '__main__':
    manager.run()

这是app.py

import os
from flask import Flask
from config import Configuration
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate,MigrateCommand
from flask_script import Manager

app = Flask(__name__)
app.config.from_object(Configuration)

db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager=Manager(app)
manager.add_command('db',MigrateCommand)

0 个答案:

没有答案