[英]sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'users'
[英]sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'houses'
啟動 API 時出現錯誤。 該錯誤表明該表沒有主鍵。 但是,我確實在數據庫中看到了主鍵。 任何想法可能會發生什么? 我想也許連接到數據庫可能出錯。 但是,在調試之后,這似乎不是問題。 這是我的代碼:
#!/usr/bin/env python3
import connexion
import logging
import os
from flask_sqlalchemy import SQLAlchemy
from swagger_server import encoder, extensions
from swagger_server.tools.config import AppConfig
def main():
env_name = os.getenv('FLASK_ENV')
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.config.from_object(AppConfig[env_name])
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres@127.0.0.1/research'
app.app.json_encoder = encoder.JSONEncoder
db = SQLAlchemy(app.app)
db.init_app(app.app)
db.Model.metadata.reflect(db.engine)
# Start the API
app.add_api('swagger.yaml', arguments={'title': 'House API'})
app.run(port=8080)
logging.getLogger('connexion.operation').setLevel('ERROR')
def shutdown_session(exception=None):
db_session.remove()
if __name__ == '__main__':
main()
models/house.py 中的 model
# coding: utf-8
from __future__ import absolute_import
from datetime import date, datetime # noqa: F401
import uuid
from typing import List, Dict # noqa: F401
from swagger_server.models.base_model_ import Model
from swagger_server.extensions import db
from swagger_server import util
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, DateTime, String, PrimaryKeyConstraint, ForeignKey, Enum, Boolean, Integer, Float, text
Base = declarative_base()
db.UUID = UUID
class House(Model, db.Model):
"""NOTE: This class is auto generated by the swagger code generator program.
Do not edit the class manually.
"""
__tablename__ = 'houses'
"""
Fields of the table
"""
house_id = db.Column(UUID(as_uuid=True), primary_key=True, server_default=text("uuid_generate_v4()"))
address = Column(String)
擴展程序.py
"""Extensions module - Set up for additional libraries can go in here."""
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
錯誤信息:
sqlalchemy.exc.ArgumentError: Mapper mapped class House->houses could not assemble any primary key columns for mapped table 'houses'
和 Postgres 表
-- Table Definition ----------------------------------------------
CREATE TABLE IF NOT EXISTS houses (
house_id uuid NOT NULL,
address varchar(256),
city varchar(128),
state varchar(64),
created_date_time timestamp with time zone DEFAULT (now() at time zone 'utc'),
updated_date_time timestamp with time zone DEFAULT (now() at time zone 'utc'),
CONSTRAINT "_pk_houses@house_id" PRIMARY KEY (house_id)
);
有什么想法嗎?
我的 class 中有額外的功能干擾了我的 model。 我可以通過將我的 class 腳本編寫為表名來完成這項工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.