简体   繁体   English

SQLAlchemy无法使用主键反映表

[英]SQLAlchemy can't reflect table with primary key

These are the classes: 这些是类:

class User(db.Model):
    __tablename__ = 'Users'
    UserID = db.Column(
        db.Integer,
        primary_key=True,
        autoincrement=True,
        nullable=False
    )
    FirstName = db.Column(db.String(255), nullable=False)
    LastName = db.Column(db.String(255), nullable=False)
    Username = db.Column(db.String(255), nullable=False)
    Password = db.Column(db.String(255), nullable=False)

class UserType(db.Model):
    __tablename__ = 'UserTypes'
    TypeID = db.Column(
        db.Integer,
        primary_key=True,
        autoincrement=True,
        nullable=False
    )
    Type = db.Column(db.String(255), nullable=False)

    __table_args__ = (
        db.CheckConstraint(
            "Type IN ('Role1', 'Role2', 'Role3')"
        ),
    )


class UserPrivilege(db.Model):
    __tablename__ = 'UserPrivileges'
    UserID = db.Column(db.Integer, primary_key=True)
    UserTypeID = db.Column(db.Integer, primary_key=True)

    __table_args__ = (
        db.ForeignKeyConstraint(
            ['UserID'],
            ['Users.UserID'],
        ),
        db.ForeignKeyConstraint(
            ['UserTypeID'],
            ['UserTypes.TypeID'],
        ),
    )

    PrivilegeUserInfoBackref = db.relationship(
        'User',
        backref='PrivilegeUserInfoBackref',
        lazy=True,
    )
    PrivilegeUserTypeInfoBackref = db.relationship(
        'UserType',
        backref='PrivilegeUserTypeInfoBackref',
        lazy=True,
    )

And here is the code for reflecting the tables: 这是反映表格的代码:

Base = automap_base()
engine = sa.create_engine(
    DATABASE_CONNECTION,
    convert_unicode=True,
    pool_size=10,
    max_overflow=20
)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base.prepare(engine, reflect=True)

The 'Users' and 'UserTypes' classes appear in Base.classes._data but for some reasson 'UserPrivileges' does not appear in Base.classes._data. 'Users'和'UserTypes'类出现在Base.classes._data中,但由于某些原因,'UserPrivileges'不在Base.classes._data中出现。 All I managed to find is that tables with no primary key can't be reflected but as you can see that is not the case here. 我设法找到的是没有主键的表无法反映出来,但是正如您所看到的,这里不是这种情况。 I also have some more tables that have composite primary key with backrefs but that are reflected with no problem. 我还有其他一些表,这些表具有带主引用的复合主键,但是可以正常显示。

So, can anyone give me any suggestions in order to reflect the last table as well, please ? 因此,请问有人可以给我任何建议以反映上一张表格吗?

The table created for UserPrivilege ticks all the boxes of a many-to-many relationship's "secondary" table , and as such is not mapped directly when using the automap extension. UserPrivilege创建的表在多对多关系的“辅助”表的所有方框中UserPrivilege ,因此在使用自动映射扩展名时不会直接映射。 This behaviour is also explained in the note of "Basic Use" : “基本用法”的注释中也解释了此行为:

By viable , we mean that for a table to be mapped, it must specify a primary key. 通过viable ,我们的意思是要映射的表必须指定一个主键。 Additionally, if the table is detected as being a pure association table between two other tables, it will not be directly mapped and will instead be configured as a many-to-many table between the mappings for the two referring tables. 此外,如果检测到该表是其他两个表之间的纯关联表,则不会直接映射该表,而是将其配置为两个引用表的映射之间的多对多表。

Your table should exist as Base.metadata.tables['UserPrivileges'] . 您的表应作为Base.metadata.tables['UserPrivileges']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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