V6__add_organizations.sql 975 B

1234567891011121314151617181920212223
  1. -- V6__add_organizations.sql
  2. -- 用户所属组织(扁平列表),users.organization_id 可空、无默认
  3. CREATE TABLE IF NOT EXISTS organizations (
  4. id INT AUTO_INCREMENT PRIMARY KEY,
  5. name VARCHAR(100) NOT NULL COMMENT '组织名称(全局唯一)',
  6. description TEXT NULL COMMENT '描述',
  7. sort_order INT NOT NULL DEFAULT 0 COMMENT '排序',
  8. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  9. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  10. UNIQUE KEY uq_organizations_name (name),
  11. INDEX idx_organizations_sort (sort_order)
  12. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户所属组织(扁平)';
  13. ALTER TABLE users
  14. ADD COLUMN organization_id INT NULL COMMENT '所属组织ID' AFTER english_name;
  15. ALTER TABLE users
  16. ADD CONSTRAINT fk_users_organization
  17. FOREIGN KEY (organization_id) REFERENCES organizations(id)
  18. ON DELETE SET NULL;
  19. CREATE INDEX idx_users_organization_id ON users(organization_id);