0

I want to make 2 tables : Teams and Projects I want to make a 1 : n relationship between these two. This is my code: //Creare tabela Projects

    public void CreareTabelaProjects() {
        string query = "CREATE TABLE IF NOT EXISTS Projects" + "(" + "id_project MEDIUMINT PRIMARY KEY AUTO_INCREMENT," + "name VARCHAR(30)," +
            "description VARCHAR(30)," + "FOREIGN KEY (team_id) REFERENCES Teams(team_id)" + ");";
        if (this.OpenConnection() == true) {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            this.CloseConnection();
        }
    }

   //Creare tabela Teams

    public void CreareTabelaTeams() {
        string query = "CREATE TABLE IF NOT EXISTS Teams" + "(" + "team_id INT AUTO_INCREMENT PRIMARY KEY," + "name VARCHAR(30)" + ");";
        if (this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand(query, connection);
            cmd.ExecuteNonQuery();
            this.CloseConnection();
        }

    }

When i run this, an error occured saying this : Key column 'team_id' doesn't exist in table. The application creates only the Team table. Any help please? Thanks!

2 Answers 2

1

Create the second table before the first one

Sign up to request clarification or add additional context in comments.

5 Comments

I am creating the Teams first
private void UserForm_Load(object sender, EventArgs e) { _dbConnect.CreareTabelaTeams(); _dbConnect.CreareTabelaProjects(); }
The table being referenced needs to exist for creating the constraint. Another way would be create tables without constraints and then alter tables to add constraints in a separate query, that will be a bit more maintainable.
I thought this is the problem string query = "CREATE TABLE IF NOT EXISTS Projects" + "(" + "project_id MEDIUMINT PRIMARY KEY AUTO_INCREMENT," + "name VARCHAR(30)," + "description VARCHAR(30)," + "FOREIGN KEY (project_id) REFERENCES Teams(team_id)" + ");"; I changed the team_id in Foreign key with Project_id. now it says : error 150. Cannot create Projects table
project_id is mediumint and team_id is int ensure both have the same datatype
1

This is the right query for Project Table

 string query = "CREATE TABLE IF NOT EXISTS Projects" + "(" + "project_id INT AUTO_INCREMENT PRIMARY KEY," + "team_id INT,"+"name VARCHAR(30)," +
            "description VARCHAR(30)," + "FOREIGN KEY (team_id) REFERENCES Teams(team_id)" + ");";

Thanks a lot anyway

2 Comments

What's the difference? BTW this problems shows why you should NOT use string concatenation to create SQL statements. Why don't you create a SQL script file and execute it on the server anyway? Or just read its contents and call execute them as a single command?
The difference is that this query actually create the key team_id in the Projects table, while the original tried to define a foreign key on team_id while team_id wasn't defined within Projects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.