I have a rails application using Postgres - I need to have some tables that have the same data in essence. So I need to have a unique key that increments for all my tables, so there is virtually a pk constraint over all the table's ids.
Now, the question is - how do I do it? can I write a migration that defines the id of all the tables to increment for each insert to any of those tables? or must I do it on the database level?
If you have such a link between two tables, you shouldn't make them have the same primary key. This is not a good use of a database.
You should instead give one of these tables a foreign key to the other one, and use this relation to identify the linked rows between the two tables.
In Rails, it's called a "has_one" relation, and it's very handy : http://guides.rubyonrails.org/association_basics.html#the-has-one-association
Related
I am trying to make one to many relationship between Contacts table and DepartmentTitle table.
I was thinking of introducing surrogate key on DepartmentTitle table so that I can reference this DepartmentTitle to Contacts table to trigger one to many relationship between these two tables. But I don't want to register same combination of the composite keys in the DepartmentTitle and that has prevented me from introducing the surrogate key to the table. I want the combination of composite keys in DepartmentTitle table to be unique.
To remedy the situation, I thought of implementing below ER diagram, where departmentTitleID would be unique and is used as reference id to the table (but is not primary key). Would this work? If not, what would be the solution?
If you're going to introduce a surrogate key, use it as your primary key. However, I would rather have Department_ID and Title_ID as separate columns in Contacts, since that allows Contacts to be joined directly to Department and/or Title as needed, without always needing to join DepartmentTitle. You can still have a composite foreign key constraint from the two columns in Contacts to the same in DepartmentTitle.
I developed a Ruby on Rails 5 application and deployed it to a production environment, after running rake db:migrate I noticed that I wasn't using foreign_key: true and neither index: true for my general relations between tables. Question is: Will this affect database performance and should I generate new migrations just to add indexes and foreign keys references to my columns? Thanks in advance.
From Ruby on Rails guide:
2.2 Schema Conventions
Active Record uses naming conventions for the columns in database tables, depending on the purpose of these columns.
Foreign keys - These fields should be named following the pattern
singularized_table_name_id (e.g., item_id, order_id). These are the
fields that Active Record will look for when you create associations
between your models.
Primary keys - By default, Active Record will use an integer column
named id as the table's primary key. When using Active Record
Migrations to create your tables, this column will be automatically
created.
You must know more about ActiveRecord Look here how it works here
I suggest you to read about belongs_to and has_many relations
Depends on what you are going to do, if you have relations where the foreign key is needed, you will need to create those migrations, otherwise you wont be able to access them.
When it comes to db performance it doesnt matter really
HTSQL works when two tables are properly linked with foreign key.
For example:
school can be easily joined with department if we have properly defined a foreign key say school_code of department table referencing code of school.
So far I'm not able to find any way to join two tables on some other columns.
Can any one help me how to do this in HTSQL?
The tweak.override extension allows you to define foreign-keys in a config file even if they're not present in the database itself. http://htsql.org/doc/admin/usage.html#extension-reference
tweak.override:
foreign-keys:
- program(school_code) -> school(code)
- program(school_code, part_of_code) -> program
I have two tables that do not have a relation defined with each other in the schema.yml.
However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.
I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:
Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c') ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');
This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.
Thanks
In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.
i'm starting now on Rails, i looked in the forum, but i didn't find anything that could solve my problem.
Here it goes, I have a Category table, and it has only name for a column (there is no repetition in categories) so i would like name to be the primary key, then i have a Product table that has name, main_photo, description and i would like to say that a product only has a category, do i need to add a column named category as a foreign key in products?
A Category is suposed to have many products.
Then in category models how do i say that name is the primary Key, and how can i do the correspondence between the suposed primary key name in categories and category in products?
Foreign key constraints in Active Record aren't used very often as the ideology behind Active Record says that this kind of logic should belong in the model and not in the database - the database is just a dumb store: http://guides.rubyonrails.org/migrations.html#active-record-and-referential-integrity.
The Rails way is to have an ID column on all tables including your Categories table, and in your Products table, have a column called Category_ID. Notice that the table names are plurals.
Then in your model you define the relationships between the entities Product and Category. Read the article A Guide to Active Record Associations and it will answer all your questions, especially sections 2.1, 2.2 and 3.3.
There are many valid reasons to have foreign keys in your database. See Does Rails need database-level constraints?
I recommend Foreigner if you want to easily add foreign keys to your Rails app.