Is there a convention for naming tables when using Doctrine ORM? I like to name tables with the plural but if there's a convention I want to stick to it.
So the table 'users' would be related to tables using the fk as the singular ('user_id').
Is there a best practice for this (using singular or plural table names) and if the latter, how does this apply to tables where the plural isn't a simple case of adding an 's'.
For example I currently have a table called 'categorys' instead of 'categories' to maintain the convention of adding 's'.
Is this a sensible approach?
I used to use plural table names when using my own basic ORM but I switched over to singular table names when I started using symfony + Propel and now a little bit of Doctrine. The reason for this is for the class names because you want to create a User and not a Users.
With Doctrine, when it comes to collections or relations, you tell it what the alias should be:
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/working-with-models/en
You'll see the a User can have many Phonenumber so a foreignAlias was setup in the YAML schema so it would be Phonenumbers effectively accessed via $user->Phonenumbers.
In your example you'll set the foreignAlias to be Categories while keeping the table and record named Category.
The Doctrine convention is to use singular names for tables and models, as the first answerer explains, because logically you want methods like:
$user->Phonenumbers
... instead of:
$user->Phonenumberss
The definitions can be customised through Aliases.
Related
Context
I have three tables in my SQL Server database: 1) School, 2) College, 3) University.
Then I have another table: Tags.
Each of the three tables (School, College, University) can have Tags associated with them. For which purpose I have three association tables: SchoolTags, CollegeTags, UniversityTags.
Problem
I am trying to create a user-defined function that will take the name of association table as parameter (i.e. 'SchoolTags') and the Id of the entity (school/college/university) and will return a list of tags associated with that entityId.
The issue I am having is I have got to join Tags with a table whose name will come in as parameter. For that I am creating a dynamic query. And we can not run dynamic queries in SQL Server user-defined functions.
Question
Any idea how can that be acheived?
Note: I want separate association tables as I have created and do not want to convert them into a generic association table and I do not want to add If-Else based on table names in my function so that if a new association table is created, I do not need to update my function.
I am using Microsoft SQL Server.
Whatever language you are using, you would probably use if:
begin
if table = 'school' then
begin
. . .
end;
else if table = 'college' then
. . .
end;
The exact syntax depends on the scripting language for the database you are using.
What you desire is impossible. You cannot pass a table name as a parameter to a UDF and use dynamic sql in the UDF to then create and execute a statement that is specific to the table passed as the argument. You already know that you have no choice but to use if-else statements in your UDF to achieve your goal - it is your pipe-dream of "never having to update (or verify) your code when the schema changes" (yes - I rephrased it to make your issue more obvious) that is a problem.
There are likely to be other ways of implementing some useful functionality - but I suggest that you are thinking too far ahead and trying to implement generic functions without a clear purpose. And that is a very difficult and trouble-prone path that requires sophisticated tsql skills.
And to re-iterate the prior responses, you have a schema problem. You purposely created three different entities - and now you want a common function to use with any of them. So before you spend much time on this particular aspect, you should take some time to think carefully about how you intend to use (i.e., write queries against) these tables. If you find yourself using unions frequently to combine these entities into a common resultset, then you have might have a mismatch between your actual business and your model (schema) of it.
Consider normalizing your database in related, logical groupings for one EducationInstitution table and one JoinEducTags table. Those tables sound like they maintain the same structure but of different typology and hence should be saved in one table with different Type field for School, College, University, etc.
Then, add necessary constraints, primary/foreign keys for the one-to-many relationship between all three sets:
You never want to keep restructuring your schema (i.e., add tables) for each new type. With this approach, your user-defined function would just need to receive value parameters not identifiers like tables to be run in dynamic querying. Finally, this approach scales better with efficient storage. And as you will see normalization saves on complex querying.
I’m in a dilemma choosing the best strategy to model my database.
Let’s say I have a two tables: Variable(ID) and Object(ID).
Now, an entry in Variable may reference another entry in Variable or in Object.
To model this, one approach is creating 2 mapping tables:
Variable_Variable(variable_id, variable_id), Variable_Object(variable_id, object_id)
The other approach is to have in the Variable table two reference columns:
Variable(ID, parent_variable_id, parent_object_id).
If this variable references another variable, then the parent_object_id is null and vice-versa.
I feel first approach is neater, but second approach is faster when querying the database.
Is there any standard to apply in this cases? Which is the usual approach for these cases?
Thanks,
Danny.
Given that all relations are 1:1 I would go with your second approach of having parent_variable_id and parent_object_id columns in your Variable table.
You could then have a CHECK constraint to ensure that only one or the other column contains a value (or neither, if your variables don't have to reference a parent).
Another alternative that you didn't mention is using a single mapping table MappingTable (variable_id, parent_variable_id, parent_object_id). The downside with this is that, if variables must have a parent, you will then have to enforce a 1:1 relationship between the Variables table and Mappings table.
I would only consider using a mapping table if modelling an n:n relationship, or if there is additional information about the relationship between a variable and it's parent that needs to be recorded.
Clearly, the implicit naming conventions for DAL/web2py are different from the conventions adopted by Rose::DB::Object but i dont see any explicit list of such conventions anywhere.
Is there a set of principles that guide the name of tables and columns when modeling data with DAL?
There are no requirements imposed, so feel free to follow whatever conventions you like. I think many of the conventions you point to are typically followed in web2py code, though there seems to be more of a preference for singular table names, and foreign key column names are often just the foreign table name, without joining "_id" to the end. By default, all tables get an auto-incrementing id field named "id", though you can change that name if you like.
I have to add functionality to an existing application and I've run into a data situation that I'm not sure how to model. I am being restricted to the creation of new tables and code. If I need to alter the existing structure I think my client may reject the proposal.. although if its the only way to get it right this is what I will have to do.
I have an Item table that can me link to any number of tables, and these tables may increase over time. The Item can only me linked to one other table, but the record in the other table may have many items linked to it.
Examples of the tables/entities being linked to are Person, Vehicle, Building, Office. These are all separate tables.
Example of Items are Pen, Stapler, Cushion, Tyre, A4 Paper, Plastic Bag, Poster, Decoration"
For instance a Poster may be allocated to a Person or Office or Building. In the future if they add a Conference Room table it may also be added to that.
My intital thoughts are:
Item
{
ID,
Name
}
LinkedItem
{
ItemID,
LinkedToTableName,
LinkedToID
}
The LinkedToTableName field will then allow me to identify the correct table to link to in my code.
I'm not overly happy with this solution, but I can't quite think of anything else. Please help! :)
Thanks!
It is not a good practice to store table names as column values. This is a bad hack.
There are two standard ways of doing what you are trying to do. The first is called single-table inheritance. This is easily understood by ORM tools but trades off some normalization. The idea is, that all of these entities - Person, Vehicle, whatever - are stored in the same table, often with several unused columns per entry, along with a discriminator field that identifies what type the entity is.
The discriminator field is usually an integer type, that is mapped to some enumeration in your code. It may also be a foreign key to some lookup table in your database, identifying which numbers correspond to which types (not table names, just descriptions).
The other way to do this is multiple-table inheritance, which is better for your database but not as easy to map in code. You do this by having a base table which defines some common properties of all the objects - perhaps just an ID and a name - and all of your "specific" tables (Person etc.) use the base ID as a unique foreign key (usually also the primary key).
In the first case, the exclusivity is implicit, since all entities are in one table. In the second case, the relationship is between the Item and the base entity ID, which also guarantees uniqueness.
Note that with multiple-table inheritance, you have a different problem - you can't guarantee that a base ID is used by exactly one inheritance table. It could be used by several, or not used at all. That is why multiple-table inheritance schemes usually also have a discriminator column, to identify which table is "expected." Again, this discriminator doesn't hold a table name, it holds a lookup value which the consumer may (or may not) use to determine which other table to join to.
Multiple-table inheritance is a closer match to your current schema, so I would recommend going with that unless you need to use this with Linq to SQL or a similar ORM.
See here for a good detailed tutorial: Implementing Table Inheritance in SQL Server.
Find something common to Person, Vehicle, Building, Office. For the lack of a better term I have used Entity. Then implement super-type/sub-type relationship between the Entity and its sub-types. Note that the EntityID is a PK and a FK in all sub-type tables. Now, you can link the Item table to the Entity (owner).
In this model, one item can belong to only one Entity; one Entity can have (own) many items.
your link table is ok.
the trouble you will have is that you will need to generate dynamic sql at runtime. parameterized sql does not typically allow the objects inthe FROM list to be parameters.
i fyou want to avoid this, you may be able to denormalize a little - say by creating a table to hold the id (assuming the ids are unique across the other tables) and the type_id representing which table is the source, and a generated description - e.g. the name value from the inital record.
you would trigger the creation of this denormalized list when the base info is modified, and you could use that for generalized queries - and then resort to your dynamic queries when needed at runtime.
This question already has answers here:
Closed 13 years ago.
Exact Duplicate
Table Naming Dilemma: Singular vs. Plural Names
Is it better to use singular or plural database table names ? Is there an accepted standard ?
I've heard arguments for and against it, what do you guys think ?
Singular, so you can have:
Customer
CustomerAddress
CustomerAddressAuditTrail
etc.
IMHO, Table names should be plural like Customers.
Class names should be singular like Customer if it maps to a row in Customers table.
I like singular names but appear to be in the minority.
My personal philosophy is that using a plural database table name is redundant, unless you're only planning for the table to contain one row.
I like to use singular names like Agent that have PK names like AgentID.
But that's just me :o)
I like to use plural forms, simply because one table contains several entities, so it seems more natural to me.
Linq to SQL converts plural form table names to singular when creating data entities. I assume that Microsoft would not have implemented this functionality if they considered plural forms for table names bad practice.
At my current company we use Plural for table names. The reasoning is this: If we have a Customers table we consider each row a Customer, so the table itself is a collection of customers.
Well, obviously your database table names have absolutely got to be named in a "standard" fashion which I will hitherto arbitrarily define.
First, all tables names shall be prefixed with "t_". Following this, the singular entity name in StudlyCaps, e.g. "Customer". Immediately afterwards, this shall contain the number of columns created in the first version of the schema, for historical purposes, followed by an underscore, and the precise normal form of the data; either "1", "2", "3" or "B" for BCNF. Any higher normal forms shall be denoted by a "P".
Some examples of acceptable names are:
t_Customer_6_3
t_Order_5_B
t_OrderLine_4_2
I think my point is, it really doesn't matter, as long as the name is reasonably descriptive and naming is consistent.
The most important thing is to be consistent in your usage. It is annoying to have to remember which tables are plurals and which are not. Same thing with your field names, pick one stadard and use it. Don't make the poor developers have to determine if this table uses person_id or personid or peopleid or person$id, etc. It is amazing the amount of time you can waste when you don't have standards trying just to remember which table uses what.
There is no should or must be this way or that way correct answer to this question. It's up to the designer of the database and software.
As for me, I usually use singular names becouse when I do the E-R diagram I have an entity Customer , not Customers, so I keep it same as to not get confused.
Ofcourse some frameworks do favor one style or another, so you should be best of to follow those practices when you notice them.
There are many arguments for each, but it all boils down to what you feel comfortable with. Neither is wrong.
What's really important is that you are consistent. Choose one standard and stick to it, which one you choose is of less importance.
IMHO it doesn't really matter, just do whatever is comfortable with you and the people that are using the database.
I think I subconsciously list main data tables with an s and "pick list" or foreign key tables and singular.
As with lots of these types of questions the best answer is often "consistent". You can argue the table represents a single entity and as such deserves a singular name, or that it contains multiple instances of an entity so it should be plural. My advice is flip a coin and go with it for the entire database (or stick with the convention that already has a majority).