简体   繁体   中英

peewee on such table

when I was useing peewee ORM, I created a postgresql database, and I made 3 script to createTable and addUser and dropTable, and it work well, but when I try to query the data in Table user, it appear: on such table : user there are some of my code: confiuration.py

class Configuration(object):
    DATABASE = 'postgresql://lc:********@localhost:5432/wolfsly'

    @staticmethod
    def init_app(app):
        pass

app.py

# -*- coding: utf-8 -*-

from flask import Flask
from .extensions import db, lm
from .configuration import config

_all_ = ['create_app']

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])

    config_blueprint(app)
    configure_template_filters(app)
    config_extensions(app)

    return app

def config_extensions(app):
    db.init_app(app)

extensions.py

# -*- coding: utf-8 -*-

from playhouse.flask_utils import FlaskDB
from flask_login import LoginManager

lm = LoginManager()
db = FlaskDB()

and one of my database script createTable.py

# -*- coding: utf-8 -*-

from application import create_app
from application.extensions import db


def createTables():
    app = create_app('default')
    from application.models import (User, Project, Photo)
    database = db.database
    database.connect()
    database.create_tables([User, Project, Photo])
    database.close()

if __name__ == '__main__':
    createTables()

when I run my createTable.py it work well and in my database appear 3 tables, and after addUser there is normal data in my User table. But when I try to get user in User table or query data in User Table it respone me "on such table: user"and in my work dir will appear a peewee.db here is some code in my auth/views.py

# -*- coding: utf-8 -*-

from flask import render_template, redirect, url_for, flash
from flask_login import  current_user, login_user
from ..models import User
from . import bpAuth

@bpAuth.route('/login')
def login():
    pass

@bpAuth.route('/test')
def test():
    query = User.select(User.id, User.chinesename)
    print 'test'
    names = [user.chinesename for user in query]
    for user in query:
        print user.chinesename
    u = User.get(User.username == 'lc')
    print u.chinesename
    return u.chinesename

and here is some screenshot

[enter image description here][1]
[enter image description here][2]
[enter image description here][3]


  [1]: https://i.stack.imgur.com/tWLYo.jpg
  [2]: https://i.stack.imgur.com/ldBRV.jpg
  [3]: https://i.stack.imgur.com/f0Tb7.jpg

and it seems when I run my webapp I can't connect my local database.

Be helpful if you shared your model definitions or the full traceback. Honestly you went through so much effort but you forgot to include the actually useful information.

For one, try in your User model to rename it to "users":

class User(db.Model):
    username = CharField()
    # etc, other fields
    class Meta:
        db_table = 'users'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM