0

Let's say I have two SQL tables address and email tables.

For the address table I may have the following generic fields:

  1. postal_code
  2. street_name

and additionally, I would want the following two fields:

  1. is_verified, of type Enum with only one of the three variants in Unverified, Verified, InProgress
  2. in_progress_description, a String of a comment on the progress status

Similarly, for the email table, I would want the following generic field:

  1. email_addr of type pydantic.EmailStr

and also the fields is_verified and in_progress_description.

Should I create a mixin like the following for the verifiability of the two tables (SQLModels) or how should I write my Email and Address classes to avoid duplicating codes for is_verified and in_progress_description fields?

=== Mixin ===

class VerifiableMixin:
    verification_status: VerificationStatusEnum = VerificationStatusEnum.Unverified
    verification_description: str = None

Then have the Email(SQLModel, table=True) subclassing it too.

class Email(SQLModel, VerifiableMixin, table=True):
    email_addr: EmailStr

=== SQLModel ===

class VerifiableBaseSQLModel:
    verification_status: VerificationStatusEnum = Field(default=VerificationStatusEnum.Unverified)
    verification_description: str = Field(default=None)
class Email(SQLModel, VerifiableBaseSQLModel, table=True):
    email_addr: EmailStr
Jim
  • 450
  • 2
  • 10

1 Answers1

1

In terms of structure and normalization, I would have a new table called status and add to both tables the foreign key. So we will get something like this:

Address

id postal_code street_name status_id
1 10001 whichever avenue 1

Status

id current description
1 Unverified situation's description

then you could have an audit table to save a history of changes if one day you will need to get some statistics for example to know the time in which each status is holding or something like this. The same ought to apply to the email table.

Nico-cac
  • 11
  • 1