So... I have 2 tables:
banner_channel
banners
I need to create a table to relate the banners (table banners) with the channels of the banners (table banner_channel).
Obs.: I cant rename table banner_channel to channel only because I already have this table in database.
Maybe, the new table is:
banners_banner_channel
or
banners_channel
? I dont know.
Anyway, thanks.
Sorry for my english... I'm brazilian 8D
First of all according to the cakePHP naming conventions the table you named banner_channel should be in plurals: banner_channels
As for the naming, it should be:
banners_banner_channels
CakePHP cheat sheat
banners_banner_channel is the correct table name according to what you have already.
just have a look at all the options in the habtm relations as you will have to set them all manually. instead of just doing 'hasAndBelongsToMany' => array('BannerChannels') you will need to set everything according to this
first example: you will need atleast the following, className, joinTable, foreignKey, associationForeignKey and with
the rest you can tweak to your needs
Related
This statement works in SQL, I just cannot figure out how to convert it to django. Im sure it uses prefetch_related, Prefetch, or select_related but im still having a hard time understanding those concepts. I do see that prefetch basically has to have the field under that table.
My goal: Not all brands have products. All products have brands. Show my only brands with products. I was hoping to implement Brand.objects.[insert-filter-here]
Model.py (appended version of actual models.py file)
class Product(models.Model):
brand = models.ForeignKey(Brand)
class Brand(models.Model):
name = models.CharField
SQL
SELECT DISTINCT products_brand.name FROM produts_brand INNER JOIN products_product on products_brand.id=products_product.brand_id;
Its 2 tables becuase the products table has many many columns (27), I guess the other option is to just
combine them. But I wanted more control over Brand objects for ease of lookup/editing.
Many thanks for your help!
It should just be Brand.objects.filter(product__isnull=False).distinct(). You can follow the foreign key relation backwards using the default reverse name (or a different one if you used the related_query_name field argument to specify one when declaring your ForeignKeyField).
Without the distinct() you may get duplicate entries.
See the "Lookups that span relationships" docs for more details and examples.
Thanks guys! I failed to mention i was using MYSQL backend and not SQLite but Peters answer got me in the right direction though, I got.
Brand.objects.values('name').distinct().filter(products__isnull=False)
I know, there already is similar questions, but I can't find a good answer at my problem.
I'm developing a social network, I have all my users in a table, but each users must be able to save multiple post from the website.
My Question is:
How can I store all the saved post for each user without creating a new table for each users. I know this is bad to do this, so I'm looking for an alternative
If I understand you correctly, you just need a middle table (for example names user_post) that have 2 columns : user_id and post_id
this is a simple implementation of a many-to-many relationship
You only need a table to store all posts call it posts table with structure like this:
|post_id|user_id|title|content|etc..|
So you can do something to save posts and identify them by user_id.
It's called one-to-many relationship, you can search more about database relationships.
I'm having conception difficulties to implement something in a database. I have two solutions for a problem, and I was wondering which one is the best.
Problem :
Let's picture a table speciality with 2 fields : speciality_id and speciality_name.
So for example :
1 - Mage
2 - Warrior
3 - Priest
Now, I have a table user with fields such as user_id, name, firstname etc ...
In this table, there is a field called speciality. The speciality stores an integer, corresponding to the speciality_id of the table speciality.
That would be acceptable for users that have only one speciality. I want to improve the model to be able to have multiple specialities for a user.
Here are my two solutions :
Create a table 'solution1' which link the user_id with the speciality_id and remove the speciality field in the user table. So for a user which has 2 specialities, 2 rows will be created in the table 'solution1'.
Change the type of the field speciality in the user table to be able to write down the specialities, separated with commas.
For example 2;3
The problem I got with the second solution is for making foreign keys between my table user and my table specialities, to link them. I may have a bit more difficulties with the PHP in the future too, while wanting to get the specilities for a user (will need to use a parser I guess).
Which solution do you find is the best ?
Thanks.
Absolutely go with your first solution.
Create a third "Many-to-Many" table that allows you to relate a user to multiple specialties. This is the only way to go in your case.
When designing tables, you always want to have each column contain one and only one data element. Think about what querying your second solution would look like. What would you do when you wanted to see all users who had a given specialty?
You might try something like this:
select * from user where specialty like '%2%'
Well, what happens when you have specialties that go to 12? Now "2" matches multiple entities. You could devolve further and try to be tricky, but...you really should just make your data design as normal as possible to avoid all the mess, headache, and errors. Go with Solution 1.
i think the best way is to follow solution1 cause solution2 will end up will lot of complexity later on
Hello I am having the following situation in my models:
class Package(models.Model):
name
class SetOfItems(models.Model)
name
class Item(models.Model):
package(FK)
setofitems(FK)
And the problem I am trying to solve is to simplify queries from SetOfItems. E.g. in case I would need to get a package name from SetOfItems, I would need to write something like
SetOfItems.objects.values('item__package__name'), which actually does not work, well in admin site (e.g. if I am creating an extra field there).
So what I am looking for is to store the information about Packages in SetOfItem model without making a direct relation (as I think that if I will put a FK there the data integrity will suffer, e.g. it will be possible to have on package inside Item and another one in SetOfItems)..
So maybe there is some possibility to have SQL view, or something like this... to have a table column automatically pre-filled from other table? Or something else?
Item is the through table in a many-to-many relationship between Package and SetOfItems. So you should declare it as such:
class SetOfItems(models.Model):
packages = models.ManyToManyField(Package, through='Item')
This doesn't change your actual table structure at all, but it allows you to do my_package.setofitems.all().
The website I'm building has a table which stores all the information of uploaded images on the site. These uploaded images can come from different resources such as a guestbook, news section or an item from an agenda.
Ofcourse I want the image to inherit the rights of the resource it is part of. For example: if user A isn't allowed to view the guestbook I don't want him to be able to view an image posted on the guestbook by going to image/view/id/12 (which would be the image request used it in the guestbook).
What I have now is that the system remembers the resources used (in this case the guestbook) the image-id is coupled to the resource-id. However I don't know to which guestbook post the image is connected (I do ofcourse know it the other way around).
Is there a way in SQL to connect one table field to a field in another table, where which table I connect to can vary based on one of the first table's field values?
In my case I would like to connect an image to a resource this could be a guestbook post in the table gb_posts or an agenda item in the table agenda_items.
Or is this all a stupid way of solving the problem and should I not use one table for the uploaded images but keep the image attached to the resource (as a column in the table for example)? It sounds like using one table is at least a lot slower in use (but I would have a great overview of all the images in one place).
I hope you guys can help me out.
EDIT: extra explanation: db model
I will try to explain how it all works the best I can.
First of all: I use Zend Framework, and therefor I also use Zend_Acl for working with priveleges.
My DB structure:
- Users are connected to roles (directly or by being connected to a group that is connected to a role)
- There is a table resources containing all the resources which is connected to priveleges. For example: guestbook is a resource, view or edit are the priveleges. Next to the controllers/actions there can also be other resources in this table such as a category within the agenda or a file location.
- roles are connected to a privelege
When for example the guestbook is requested for viewing I can check if the user is allowed to.
In short something like:
users -> roles -> priveleges <- resources
When a user adds a guestbook post with an image, the used resources (in this case guestbook is saved):
guestbook_posts -> images -> resources
I hope this explains my DB model for a bit, if it doesn't I will try to create an image of the tables.
I have to admit I'm failing to completely understand the model you wish to implement, but there is an interesting quote...
However I don't know to which
guestbook post the image is connected
(I do ofcourse know it the other way
around).
If you know an association one way, you should be able to use the associaton in both directions? I'm assuming you have a table that includes "post_id, image_id", or something?
It may be that the table is only indexed post_id first, in which case querying that table by image_id may be slow, but then you can just include a new index with image_id first?
If you can give examples of the table structure you have at present, and an example of the query you can't fullfil, we may be able to help you further.
Sounds like you want a foreign key constraint.
Update: Completely misunderstood the question, apparently.
There are two approaches here:
As it currently stands, there is nothing in the schema that would prohibit linking the same image from multiple resources. If that is desired, then a foreign key constraint and an index for the backreference is probably the best solution, although it will not scale well, and requires additional computation (because the rights on the image need to be the union of the rights of the refering resources).
The alternative is to create some kind of inheritance schema, where there is a table listing "resources" (that effectively just contains identifiers) that is referenced as a foreign key from the actual resource tables and the images table; the only constraint that cannot be expressed in plain SQL is that different resources may not share the same identifier.
Create two SELECT clauses, each having the correct joins to the correct tables, and then combine the output of the two SELECT clauses together using a UNION statement.
SELECT field1, field2
FROM table1
JOIN table2 on table1.PK = table2.FK
WHERE table1.selector = 1
UNION SELECT field1, field2
FROM table1
JOIN table3 on table1.PK = table3.FK
WHERE table1.selector = 2