how to configure Model Options - cakephp

I want to reverse engineering a DB I create for the CakePHP framework.
So the naming convention is this one:
Tables are always named in plural.
If users belongsTo or belongsToMany users, relationships are declared like:
users owners
id
owner_id --> id
If sites hasAndBelongsToMany users, relationships are declared like
sites sites_users users
id
id <-- site_id
user_id --> id
So I would like to configure Model Options in MySQLworkbench in order to create diagram.
So I tried to set the following options but it doesn't work:
Column Defaults:
PK Column name: id PK Column Type: INT
Column name: %col Column Type: VARCHAR(45)
Foreign Key/Relationship Defaults:
FK name: %dtable%_id Column name: %dtable%_id
ON UPDATE: NO ACTION ON DELETE: NO ACTION
Associative Column name: %stable%s_%dtable%s
Is it the right way to do what I want?

Related

CakePHP - Second Level Model Association Naming Convention

I have Project, ProjectImage, and ProjectImageCategory.
a Project hasmany ProjectImage and a ProjectImage belongs to a Project
a ProjectImageCategory hasmany ProjectImage and a ProjectImage belongs to a ProjectImage
How should I name the model classes respectively the database tables and foreign keys, so that CakePHP would bind them.
Thanks
Project model use projects table and ProjectImageCategory use project_image_categories table and ProjectImage use project_images table, right?
the FK is ta name of the model with slug +_id , for example Project , to use a FK with this model you need set project_id , if project hasMany ProjectImage, in your project_images table you need a project_id column.

SQL Server Define Relationship between the same table

I would like to know if it's ok to define a relationship for a column of a table and say it's a foreign key of the same id column from the table.
Example:
Tbl. Name: Categories
Column 1: id
Column 2: name
Column 3: parent_id
Parent_id should contain (only if it's necessary) the id of the category.
I want to make a tree structure of my categories.
Is it ok to do this?
Yes, when it is appropriate. Which it is in the case you describe for a Parent-Child relation of the same table.

Microsoft Access 2013 - Multiple Foreign Keys in the same Record

I’ve encountered a problem with pulling multiple foreign keys into one record in a Microsoft Access table.
I’ve got two tables with these fields:
Table 1:
Owners
Fields:
Owner ID (Primary Key)
First Name
Last Name
Table 2:
Ships
Fields:
Ship ID (Primary Key)
Ship Name
I need to create a relationship between Table 1 and Table 2 which shows who owns the ship. I’ve tried adding a number field to Table 2 called Owner ID (Foreign Key) and this works absolutely fine.
Working Example:
Table 1 – Owners Table 2 – Ships
Owner ID (Primary Key)__ Ship ID (Primary Key)
First Name \ Ship Name
Last Name \________Owner ID (Foreign Key)
Unfortunately my ships in Table 2 can have multiple owners (up to 5) at the same time. My problem arises when I try to create a series of linking fields in Table 2.
Not Working:
Table 1 – Owners Table 2 – Ships
Owner ID (Primary Key)__ Ship ID (Primary Key)
First Name \ Ship Name
Last Name \ Owner1 ID (Foreign Key)
\______/ Owner2 ID (Foreign Key)
\ Owner3 ID (Foreign Key)
Can anyone recommend any workarounds so I can show multiple owners taken from the Owners table in the Ships table?
Thanks for your help!
Your database design is definitely incorrect.
In the case you explain, you have a many-to-many relationship between Ships and Owners, which MUST translate into a "relationship table" in the relational model.
In this case: a [Ownership] table, with 2 fields, being the 2 Primary Keys (PK) of the related tables.
In other words, you need 3 tables:
Ships(ShipId, ShipName, Whatever) PK is ShipId
Owner(OwnerId, FirstName, LastName) PK is OwnerId
OwnerShip(ShipId, OwnerId) PK is made of the 2 FKs
The problem is that it looks like Access doesn't allow Nullable FK's and so all Owner fields would have to be filled in, no matter how many owners there are.
The only solution to this I can think of is to introduce a ShipOwner table, which has ShipID and OwnerID columns (as FK's to the Ship and Owner tables). You can then have as many Owners as you like.
Pros: You can add things like %Owned if that matters
Cons: The software has to enforce the limit of 5 owners
Biggest Pro: it will work!
Cheers -
EDIT: The first para is wrong: Access does let you add nullable FK's. However I still thing the suggestion here is a good one. Repeating Groups (Owner 1 to 5) is against Normalisation rules, and this suggestion is normalised.

Database normalization and duplication

I have a many to many relationship between bookmarks and tags:
Bookmarks
Id
UserId
Title
Url
Tags
Id
UserId
Title
Description
TagsBookmarks (junction table)
TagId
BookmarkId
As far as I know this is normalized and valid.
However it is possible that multiple users save the same bookmarks which means duplicate URLs in the bookmarks table. Does this kind of duplication belong to normalization and should it be avoided?
Update:
The system works like this:
User logs in and create bookmarks / tags.
A bookmark should have atleast one tag attached to it. For example: "http://google.com" may belong to tag "google" and "search-engine".
When a bookmark gets deleted the relation in TagsBookmarks also get deleted. The tags do not get deleted.
A user can delete a tag if it has no bookmarks.
A bookmark cannot exist without a tag.
A tag can exist without bookmarks.
Usually, table names and table columns are singular.
I would move UserID to TagBookmark.
Bookmark
--------
BookmarkID
BookmarkTitle
BookmarkURL
Tag
---
TagID
TagTitle
TagDescription
TagBookmark
-----------
UserID
TagID
BookMarkID
The primary (clustering) key of TagBookmark would be all 3 ID fields concatenated.
I'm not sure what the difference is between a TagTitle and a BookmarkTitle, so there may be some further normalization to be done.
You should exclude UserId from Bookmarks and Tags tables and add it to your junction table, because your system is user-centric as I understood from your explanation. Like this:
Users
Id
FirstName
...
Bookmarks
Id
Title
Url
Tags
Id
Title
Description
UsersTagsBookmarks (junction table)
UserId
TagId
BookmarkId
or even you need two junction tables, but it depends on your system logics:
TagsBookmarks (junction table 1, specifies all possible tags/bookmarks combinations)
Id
TagId
BookmarkId
UsersTagsBookmarks (junction table 2)
UserId
TagBookmarkId

Composite foreign key in SQL Server

I have a table named Books which contains 3 columns.
TableName: Books
Columns: BookId (PK), BookName, Book_Publisher_XRef_Id (FK), IsInternal
I have two tables that contains publisher information. Both these tables have different set of columns.
TableName: InternalPublishers
Columns: PublisherId (PK), PublisherName, ....
TableName: ExternalPublishers
Columns: PublisherId (PK), PublisherName, ....
I have a link table that contains information about which book belongs to which publisher. One book can have multple publishers.
TableName: Books_Publishers_XRef
Columns: Book_Publisher_XRef_Id (PK), PublisherId
If I want to create a Foreign Key constraint on PublisherId, I need to create sort of Composite Foreign Key constraint which I am not sure can be created.
So in this scenario, what is the best way to achieve FK on PublisherId in Books_Publishers_XRef table?
Break Books_Publishers_XRef table in 2 tables i.e. one for Internal Publishers and another one for External Publishers and have 2 columns in Books table for Books_Internal_Publishers_XRef and Books_External_Publishesr_XRef tables?
Don't create FK on Publisher_Id column and leave the design as it is?
Create composite FK by adding Publisher_Type_Id column in Books table and Books_Publishers_XRef table where if Publisher_Type_Id = 1, it belongs to Internal_Publishers table and Publisher_Type_Id = 2, it belongs to External_Publishers table ? (Not sure if this is possible)
Some other schema design?
Please advise.
Don't divide your data amongst two tables: InternalPublishers, ExternalPublishers. Create one table and have a bit field to determiner whether they are internal or external. Something like this:
create table Publisher
(
PublisherId int not null primary key clustered,
PublisherName varchar(100) not null,
IsInternal bit not null
)
go
That way you can easily create your foreign key reference. After all, you seem to have this same design for Books, keep that going to publishers.
Keep all common columns in the Publisher table.
Subtype tables have only columns specific to each one.

Resources