Skip to content
Advertisement

Is it possible to create custom “Columns” in SQLAlchemy? is this breaking OOP?

I’m new in SQLAlchemy and I’m trying to understand some concepts.

Here is a code example without SQLAlchemy:

How can I move this to SQLAlchemy?. I think I have to do something like:

But, when I get the User, token will be a String instead of a Token object. Can I create a Column with my objects?. Example:

If I can’t do this, all my objects that use a database must have only “simple” variables (string, int, etc). I think this breaks OOP, right?.

Advertisement

Answer

When defining columns in a model (class UserDatabase), you are limited to types that exist in the database engine being used by you.

However, some database engines allow you to overcome this difficulty.

In PostgreSQL it is possible to define your own custom types, either by pure SQL or with the usage of ORM such as SQLAlchemy.

source: SQLAlchemy Docs

As you can see it is implementing additional logic on top of already existing types, therefore it is limited.

But for your use case it should be sufficient – you can make your own type on top of varchar type and perform some logic to token-ify that string.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement