【问题标题】:Error creating tables in MySQL using peewee and FastAPI使用 peewee 和 FastAPI 在 MySQL 中创建表时出错
【发布时间】:2021-08-19 21:55:30
【问题描述】:

我有以下基本代码,我正在尝试使用 peewee ORM 在 food_delivery 数据库中创建表。我收到如下错误:

AttributeError: 'str' 对象没有属性 'safe_create_index'

我的代码如下:

DATABASE = 'food_delivery'
db = MySQLDatabase(
    host='localhost',
    user='my_user',
    password='****',
    database=DATABASE
)

class BaseModel(Model):
    class Meta:
        database = DATABASE

class Customer(BaseModel):
    city = CharField()
    customer = AutoField(column_name='customer_id')
    email = CharField(column_name='email_id',unique=True)
    first_name = CharField()
    landmark = CharField()
    last_name = CharField()
    password = CharField()
    phone_no = CharField(max_length=10)
    pincode = IntegerField()
    state = CharField()

    class Meta:
        table_name = 'customer'
        database = DATABASE
def create_tables():
    with db:
        db.create_tables([Customer])
create_tables()

有人可以建议代码可能有什么问题吗? 谢谢!

【问题讨论】:

    标签: mysql python-3.8 fastapi peewee


    【解决方案1】:

    我一直在寻找问题,发现我基本上有一个逻辑语法错位。而不是 META 中所有表定义下的数据库名称,实际上我必须传递数据库的连接实例而不是数据库名称。但我想知道抛出的错误有多么不合适,而且看到错误详细信息是不可能的。这需要库维护者进行大量改进。

    【讨论】:

      猜你喜欢
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多