Postgres Custom Type vs Table - postgresql

first time I model a database in quite some time and I see that Postgres has the ability to define custom enum types very similar to enums in programming language.
My question is when should types be used instead of a table with foreign key ?
In a column like "sex" it looks obvious a type is good choice, but should I do this for all "lookup" tables ? Nationality ? Employee Status etc ?
Thank you for your comments and thoughts!

Related

Migrate inheritance to database in different tables

I've made this structure in my models:
And I'll update my database like show below:
But if I add a migration and I update my database, I become this:
How could I migrate my classes to the database structure I will?
The table below is correct because the field Discriminator is a nvarchar variable that decided if the object is a topic, blog or comment. The fields upvote and downvote will be null if the topic is a comment, otherwise it will be not null.
In other words, it is not necessary to split the tables in your database. If I look to your class diagram I don't see any many to many relation.

EF Nullable Foreign Key Relationship

I need some help today related to EF relationship. I have two lookup tables Country and Ethnicity. I want to have nullable foreign keys for both in one my table named Singles, so I defined a relationship in my class like
Single Relation
that generate a table like this, which is good so far
Single Relation Result
But I have other fields like Citizenship and CountryOfBirth which require a foreign key as well from Country table. So, I tried to do the same
Multiple Relation with Same Class
But things getting weird inside sql when table created.
Multiple Relation with Same Class Result
I can understand why it behaves odd but don't know how to make it work. Can you please suggest?
Thanks
You'll need to place your ForeignKey attributes on the navigational properties to point to the nullable ID field (instead of vice-versa), and then use the InverseProperty attribute to properly tell EF exactly what kind of relationship you are trying to accomplish.
This answer will be quite similar to that in this SO question.

What does "ptr" stand for in Django ORM column names?

With these Django ORM models:
class A(Model):
pass
class B(A):
pass
The table for B in the resulting schema contains the field:
"a_ptr_id" integer PRIMARY KEY
Just for the sake of better understanding Django's design choices - It there any rationale documentation that mentions what "ptr" signifies?
My only guess is that it's an abbreviation for "pointer", but that seems pretty dumb since essentially any field with a foreign key constraint conceptually resembles a pointer.
It does stand for "pointer", but it's only used specifically for model inheritance in order to point to the parent model. Normal FKs only get "_id".

How to decide a Primary Key from an Entity Collection programmatically

I'm encountering some problems importing my DB on Entity Framework. Some relationship between tables returns me some other entities without any primary key.. For my application I need to have a PK for every table.
Is there any way to say something like:
MyEntityCollection.SetKey("ColumnName")
Or something like that?
Thank you very much!
Take a look here, i discuss this in this answer,
EF4 Unknown Column In Field List
http://xhalent.wordpress.com/2011/01/21/configuring-entity-framework-4-codefirst/ has some detail around the two ways in EF4 of specifying a FK

How to Use inheritance in EF

I am using EF 4.0 , i have one problem
Table structure in DB is:
Table: Setting--->
Name (PK)
GroupBy
DataType
Table: UserSetting-->
SettingName(PK)(FK)
UserName(PK)(FK)
Value
Table: WorkstationSetting-->
SettingName(PK)(FK)
WorkstationName(PK)(FK)
Value
Now i want to make use of inheritance, because WorkstationSetting and UserSetting inherits settings so any suggestion how to achieve inheritance, i tried but i got error like
"Error 39 Error 3003: Problem in mapping fragments starting at line 1621:All the key properties (Settings.Name) of the EntitySet Settings must be mapped to all the key properties (WorkstationSetting.SettingName, WorkstationSetting.WorkstationName) of table WorkstationSetting.
I see you have in UserSetting and WorkstationSetting a composite PK.
If UserSetting and WorkstationSetting are derived from Setting, they should have Name as PK.
Another comment; in general, it's not recommended to use a name or something "meaningful" as PK since it is less scalable and might cause limitations (i.e. max index size). Use instead an int or uniqueidentifier.
I recommend you to introduce a new field which is SettingId which should be added to all three tables. In EF designer, just add the Inheritance.
Look into table per type inheritance. For example look here. It should help you get started. The idea is that you have a table for each concrete type (as you have) and you map it to an object hierarchy.
Maybe your problem is with the keys. How is your mapping defined? Are the associations between the tables defined in the DB?