简体   繁体   English

sqlite 当你用外键删除一行时 id 会发生什么?

[英]sqlite what happens to id when you delete a row with a foreign key?

I have these two tables:我有这两张表:

class User(Base):
    """ User entry in database """
    __tablename__ = "users"
    # ID uniquely identifies 
    id             = Column(Integer, primary_key=True, index=True)
    kerberos       = Column(String, unique=True, index=True)
    hashpass       = Column(String)
    is_active      = Column(Boolean, default=True)
    creation_date  = Column(DateTime, default=None)

    scores = relationship("Score", back_populates="owner")

class Score(Base):
    """ Stores each users scores """
    __tablename__ = "scores"
    
    id    = Column(Integer, primary_key=True, index=True)
    user_id = Column(String, ForeignKey("users.id"), index=True)    
    score = Column(Integer)
    time = Column(DateTime)
    
    owner = relationship("User", back_populates="scores")

I have read that adding new "Users" will auto-increment the "id" field.我已经读过添加新的“用户”将自动增加“id”字段。 Now each entry in the "scores" table has a foreign key pointing to users.id.现在,“scores”表中的每个条目都有一个指向 users.id 的外键。 This means (correct me if I'm wrong), that each "score" entry maps to one and only unique User (so the score:user relationship is many:one).这意味着(如果我错了,请纠正我),每个“分数”条目都映射到一个且唯一的唯一用户(因此分数:用户关系是多:一个)。

I'm wondering if this is a good design, especially if I delete a user, what happens?我想知道这是否是一个好的设计,特别是如果我删除一个用户,会发生什么? Does deleting a user automatically invalidate/delete the corresponding scores that are linked thru the deleted user's id (foreign key for those scores)?删除用户是否会自动使通过已删除用户的 id(这些分数的外键)链接的相应分数无效/删除? I am concerned that deleting a user will all user id's afterwards to shift down by one, so there would be an off by one issue when looking at a user's scores.我担心删除用户之后所有用户 ID 都会向下移动一个,因此在查看用户的分数时会出现一个问题。

Is this good design?这是好设计吗? Feel free to link resources/redirect to another question (I couldn't not find any specifically)随意链接资源/重定向到另一个问题(我找不到任何具体的问题)

I'm wondering if this is a good design, especially if I delete a user, what happens?我想知道这是否是一个好的设计,特别是如果我删除一个用户,会发生什么?

As it stands you would not be able to delete the User(s) as a Foreign Key Conflict would result.就目前而言,您将无法删除用户,因为会导致外键冲突。 If you didn't have the FK constraint, protecting the referential integrity, then you would orphan the Scores (they would have no parent) which could result in unanticipated results and perhaps crashes.如果您没有 FK 约束来保护参照完整性,那么您将孤立分数(它们将没有父级),这可能导致意外结果并可能导致崩溃。

To be able to delete a User, I believe that you could use ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE")为了能够删除用户,我相信您可以使用ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE")

Then deleting a User (parent) will then CASCADE the deletion, thus deleting the children (Scores that link to that specific parent) due to onDelete="CASCADE" .然后删除用户(父级)将 CASCADE 删除,从而由于onDelete="CASCADE"而删除子级(链接到该特定父级的分数)。

Should you ever change a User's id value, then the user_id in the children would be changed (ie the update to the id will CASCADE down to the children).如果您曾经更改用户的 id 值,那么子项中的 user_id 将被更改(即对 id 的更新将向下级联到子项)。

I am concerned that deleting a user will all user id's afterwards to shift down by one,我担心删除用户之后所有用户 ID 都会向下移动一个,

No they will not, unless you make them (which you would probably would not want to do).不,他们不会,除非您制作它们(您可能不想这样做)。 However, with the onUpdate action as CASCADE if id's were changed the changes to the id would be CASCADEd.但是,如果将 onUpdate 操作作为 CASCADE,如果 id 被更改,则对 id 的更改将被 CASCADEd。

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

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