I am new to using GORM and have inherited some code that is linked with postgresql.
In the "models" folder, there are 2 existing tables, using a single foreign key relationship on the 2nd table.
The foreign key doesn't specify which table the foreign key relationship is pointing to, however in this specific example there are only 2 tables.
Therefore, perhaps it can be assumed that the foreign key must point to the second table?
type Criteria struct {
Name string `gorm:"primaryKey; default:null; unique;not null" json:"name" uri:"criteria_name"`
Created_At time.Time `json:"article_url" uri:"-"`
Article_Url string `uri:"thing" test:"testing" gorm:"unique;not null" json:"test_Article_URL" uri:"-"`
Location string `json:"Location" uri:"pID" gorm:"primaryKey"`
}
type CriteriaVersions struct {
Name string `json:"name" gorm:"primaryKey" uri:"criteria_name" gorm:"foreignKey:Name,Location"`
Version_Number int `json:"number" gorm:"primaryKey" uri:"version_number"`
Source datatypes.JSON `json:"source" uri:"-"`
Created_At time.Time `json:"-" uri:"-"`
Diagnosed_Updated_At time.Time `json:"-" uri:"-"`
Location string `json:"practice" gorm:"primaryKey" uri:"Location"`
}
As can be seen, the foreign key doesn't specify what table it is pointing at.
So, if I added a new table with something like:
type Person struct {
Name string `gorm:"primaryKey"`
Location string `gorm:"primaryKey"`
}
What is linking the 2 first tables? As if I try and add a new Criteria Version, then this is still being validated against the Criterias table.
To clarify, I do not want to change the reference to point to the Doctors table, but I want to understand how it chooses which table to reference.
For example, if I have a table with:
Criterias
Name
Article_Url
Location
Name1
www.google.com
America
Name2
www.bing.com
Germany
and another table with:
Doctors
Name
Location
Doctor Who
Gallifrey
Doctor Do-Little
USA
Doctor Hibbert
Springfield
and I try to add a new Criteria Version containing:
name: "Doctor Who", location: "Gallifrey"
This is only validated against the Criterias table, and is therefore rejected:
ERROR: insert or update on table "criteria_versions" violates foreign key constraint "criteria_versions_name_fkey" (SQLSTATE 23503)
Whilst if I add a new Criteria Version containing:
name: "Name1", location: "America"
This does successfully add to my Criteria Versions table.
Any explanation is appreciated, as well as advice on making it more verbose (I think this is via using references, but I wanted to understand this bit first)!
Related
When I create my tables in the gorm database, it adds columns to the table that I don't want. I'm not sure how it's adding these extra fields. This causes me to run into an error that says, "pq: null value in column "user_id" violates not-null constraint". "user_id" is the unwanted column that gets added. I'm using gorm and postgreSQL.
I have a many to many relationship for my two tables. My first table is created properly and my second table, stores, is created with the provided fields plus two unwanted fields: "user_id" and "stores_id". I've tried removing the many to many relationship to see if that was the issue, I've tried dropping the tables and recreating them with different fields. Regardless, I have not been able to get rid of the two extra columns.
The first (working) table:
type User struct {
gorm.Model
ID int `json:"u_id"`
Name string `json:"u_name"`
Stores []Store `gorm:"many2many:stores;" json:"stores"`
}
When I execute '\d users', I get the following columns: id, created_at, updated_at, deleted_at, name.
The second (problematic) table:
type Stores struct {
gorm.Model
ID int `json:"s_id"`
NUM int `gorm:"unique" json:"s_num"`
Users []User `gorm:"many2many:user" json:"users"`
}
When I execute '\d' stores, I get the following columns: user_id, vehicle_id, id, created_at, updated_at, deleted_at, num.
I'm executing the creation of these tables through the following code:
db.AutoMigrate(&User{})
db.AutoMigrate(&Store{})
On another note, if I add gorm:"primary_key";auto_increment" to my ID values in my structs, I get the error "pq: column "user_id" appears twice in primary key constraint". I was able to get around this by removing the primary_key and auto_increment attributes, running AutoMigrate() and then adding it back in and running AutoMigrate() again - this was totally fine and working.
I've also tried manually inserting a user_id and store_id. This works fine, except that I'd have to generate new ones every time because they require uniqueness. I understand that the error "pq: null value in column "user_id" violates not-null constraint" is caused by the fact that I'm not providing a user_id or store_id when I'm creating my store. I'm simply confused why a user_id and store_id column is being generated at all, and I'm hoping I can fix that.
This is what gorm.Model looks like
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
}
When we call gorm.Model inside a struct, it means we are add default fields of gorm.Model in our current struct.
type Stores struct {
gorm.Model
....
so your user model will look something like
ype User struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
ID int `json:"u_id"`
Name string `json:"u_name"`
Stores []Store `gorm:"many2many:stores;" json:"stores"`
}
that error mayne due to duplicate primary_key key. try to rename ID intjson:"u_id"`` to UserID. you need to update Stores too.
Fixed the duplicate ID errors by removing gorm.Model, as #(Akshaydeep Girl) pointed out what having gorm.Model entails. As for the random 'user_id' and 'store_id' that kept automatically being added, they were being added because of the many2many gorm relationship. I was able to remove those by switching the order of migration.
func DBMigrate(db *gorm.DB) *gorm.DB {
db.AutoMigrate(&Store{})
db.AutoMigrate(&User{})
return db
}
When I dropped both tables and re-compiled/ran my project with the new order of migration, the stores table was created without the random 'user_id' and 'store_id', and the users table didn't have those either.
Someone else asked a similar question here: How can I use EF6 to update a many to many table
I mention that up front because I couldn't get any of the solutions given to work.
I also studied the solution give on Code Project: http://www.codeproject.com/Tips/893609/CRUD-Many-to-Many-Entity-Framework, but this doesn't work for me either.
I'm trying to keep this as simple as possible.
I have two tables: dbo.Teacher and dbo.Student. Each has an "ID" column that servers as a primary key. I also have a third table called dbo.StudentTeacher which has exactly two columns, both are non-nullable and foreign keyed to the previous two tables; in other words, it establishes a many-to-many relationship between teachers and students. As expected, the EDMX designed shows only dbo.Student and dbo.Teacher and infers the relationship between them.
Here is a script for the above; there is nothing else in the database.
CREATE TABLE dbo.Teacher
(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(MAX)
);
CREATE TABLE dbo.Student
(
Id INT NOT NULL PRIMARY KEY,
Name VARCHAR(MAX)
);
CREATE TABLE dbo.TeacherStudent
(
TeacherId INT NOT NULL FOREIGN KEY REFERENCES Teacher(Id),
StudentId INT NOT NULL FOREIGN KEY REFERENCES Student(Id)
);
INSERT INTO Teacher(Id, Name)
VALUES
(101, 'Tom');
INSERT INTO Student(Id, Name)
VALUES
(201, 'Sue'),
(202, 'Stan');
INSERT INTO TeacherStudent(TeacherId, StudentId)
VALUES
(101, 201);
Now that I've established my data structures, I want to accomplish a very simple task. From the script above, you can see that we have one teacher named "Tom" who has a student named "Sue". We also have a student named "Stan" with no teacher. My task is to modify the database so that Sue is no longer Tom's student and Stan becomes Tom's student.
To accomplish this, I wrote the following code:
class Program
{
static void Main(string[] args)
{
using (var entities = new TestEntities())
{
// There is only one teacher in the system.
Teacher teacher = entities.Teachers.Single();
// This teacher has a student #201: Sue.
// I want to replace her with student #202: Stan.
teacher.Students.Clear();
teacher.Students.Add(new Student() { Id = 202 });
entities.SaveChanges();
}
}
}
It looks very simple: clear the students associated with Tom and then add Stan as Tom's student. However, when I run the code, I get the following error: Unable to update the EntitySet 'TeacherStudent' because it has a DefiningQuery and no <DeleteFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
I tried simplifying the problem by trying to just remove Sue from being Tom's student without adding Stan, and I get the exact same error message.
As I understand, this error normally occurs when Entity Framework doesn't have enough information to do what you want it to do, but I really can't see what's missing. There are two simple tables with a join table between them and I need to be able to change which rows are related to which other rows.
I should also note that if I'm not mistaken, the change that I wish to make in this example should affect only the dbo.TeacherStudent table; the other two tables should not be touched.
Okay, after some more Google-Fu, I figured it out.
Even tho the join table must have only two columns with each column foreign keyed to the two tables to be related, the join table still needs a primary key, which can be a composite of the two foreign keys.
Thus, dbo.TeacherStudent should be created with this:
CREATE TABLE dbo.TeacherStudent
(
TeacherId INT NOT NULL FOREIGN KEY REFERENCES Teacher(Id),
StudentId INT NOT NULL FOREIGN KEY REFERENCES Student(Id),
PRIMARY KEY(TeacherId, StudentId)
);
I have my Entity model where every entity has a primary key and they are marked with StoreGeneratedPattern = "Identity", the entity I am interested in is PEOPLE. It was created in oracle, and in another schema exists a "PEOLPE_HIST" table with it sequence. Before insert into PEOPLE I search on PEOPLE_HIST and if the person exists. I get the history information to including the ID (primary key) to insert into new PEOPLE table. I created a trigger for PEOPLE table to check if the ID is greater than or equal to 0, if it is, then I will not use the sequence and insert the Hist ID.
Is this possible?
thanks in advance
Hello
I have a main table BASECOMPANYDATA with BaseCompanyDataID as a PK. This is inhereted by
2 other tables CUSTOMERS & PRODUCTCOMPANIES. Now I have the table CONTACTS which I
want to connect with the 2 others as the customers and the productcompanies will have 0 or
more contacts. So I made a FK(BaseCompanyID) in CONTACTS and connected to the
BASECOMPANYDATA PK(BaseCompanyDataID). But when I am trying to insert a contact for
a record which exists in CUSTOMERS I get the following error:
ERROR: insert or update on table "xxxxx" violates foreign key contrain "xxxxx"
DETAIL: Key (BaseCompanyDataID)=(17) is not present in table "BaseCompanyData".
This ID exists in the above inherited table (BaseCompanyData).
Can someone explain why is this happening?
Thanks in advance
PS:Well, I have 4 tables:
1.BASECOMPANYDATA with BaseCompanyDataID as PK and some other fields.
2.CUSTOMERS which inherits from the above table so it has CustomerID as PK and has the fields of the BASECOMPANYDATA table namely BaseCompanyDataID etc.
3.PRODUCTCOMPANIES which inherits from BASECOMPANYDATA so it has the fields ProductCompanyID as PK and the fields of the inherited table like BaseCompanyDataID etc.
4.CONTACTS with ContactID as PK and BaseCompanyDataID as a FK. I tried to connect the table CONTACTS with 2 different ways. a. CONTACTS->BaseCompanyID with CUSTOMERS->BaseCompanyDataID and CONTACTS->BaseCompanyID with PRODUCTCOMPANIES->BaseCompanyDataID b. CONTACTS->BaseCompanyID with BASECOMPANYDATA->BaseCompanyDataID The result was the same error. Any answer on how I can create the FK using the inheritance, if there is. Thanks in advance
Did you read through the inheritance docs? Especially the 5.8.1. Caveats section?
http://www.postgresql.org/docs/9.0/static/ddl-inherit.html
...
Similarly, if we were to specify that cities.name REFERENCES some other table, this constraint would not automatically propagate to capitals. In this case you could work around it by manually adding the same REFERENCES constraint to capitals.
edit:
Inheritance is only half implemented in Postgsresql. If you want to save typing check out like in create table
In your first question I see the person recommended exactly the same thing I said. And now you have a problem? Hmm ...
This is pseudo sql I get from your repost:
base
baseid
customers(base)
baseid
id
products(base)
baseid
id
contacts
id
baseid references base(baseid)
Just do it the good old fashioned way!
base
id
customers
base_id references base(id)
id
products(base)
base_id references base(id)
id
contacts
id
base_id references base(id)
I've got the following entities on my EDMX :-
These two entites were generated by Update Model From Database.
Now, notice how my country has the following primary key :-
Name & IsoCode
this is because each country is UNIQUE in the system by Name and IsoCode.
Now, with my States ... it's similar. Primary Key is :-
Name & CountryId
Each state is unique by name and per country.
Now, the Foreign Key for States is a CountryId. This is the sql :-
ALTER TABLE [dbo].[States] WITH CHECK ADD
CONSTRAINT [FK_States_Countries] FOREIGN KEY([CountryId])
REFERENCES [dbo].[Countries] ([CountryId])
ON UPDATE CASCADE
GO
ALTER TABLE [dbo].[States] CHECK CONSTRAINT [FK_States_Countries]
GO
Pretty simple stuff.
BUT EntityFramework doesn't like it :( It's assuming that i need to connect some properties from State entity to both primary key properties in the Country entity.
Is it possible to add an ASSOCIATION between Country and State on Country.CountryId <-> State.CountryId ... like i have mapped in my DB ?
Cheers ;)
In EF (3.5 and 4.0) FKs MUST point to Primary Keys.
But you appear to be attempting to point to a Candidate Key (i.e. [Countries].[CountryId]
I know that this is something the EF team are considering for the next version though :)
Hope this helps
Alex
For proper DB normalization, first thing is that primary keys must be only CountryId and StateId fields - the main Id fields for each table.
And ss I see from the description Name & IsoCode and Name & CountryId should be actually Unique keys, not primary.
Then the model class State should have a field:
public Country Country { get; set; }
Now EF have very good examples and since 4.3.1 + it fully supports Code first / DB first models, which I think will ease solving this.
EF 5 have more compatibility updates so I think it wont be a problem for legacy DB engines.