我正在尝试使用 SqlAlchemy 在 MySql 中创建一个自引用表。我收到错误 TypeError:附加参数应命名为 _,得到“ForeignKey”
我的代码是
from sqlalchemy import (
create_engine,
Column,
Integer,
String,
DateTime,
func,
Text,
DECIMAL,
ForeignKey,
)
# from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, declarative_base, mapped_column, relationship
Base = declarative_base()
class Account(Base):
__tablename__ = "account"
id = mapped_column(Integer, primary_key=True)
name = mapped_column(String(50))
notes = mapped_column(Text())
opening_balance = DECIMAL()
current_balance = DECIMAL()
parent_id = mapped_column(Integer, ForeignKey="account.id") #error here
children = relationship("Account")
created_at = mapped_column(DateTime, default=func.now())
updated_at = mapped_column(DateTime, default=func.now(), onupdate=func.now())```
the error occurs at the line parent_id = ...
everywhere I look suggests that the code is correct (and I've looked in a lot of places). Co-Pilot told me to put () around "account.id" but PyCharm told me they were redundant. Either way got the error. Tried changing mapped_column to Column, same error.
Havent got a clue what to do!