From 339e6e6b025d27261da063f3176352d752b22d45 Mon Sep 17 00:00:00 2001 From: Mauriciofearauj Date: Mon, 20 May 2024 02:57:35 -0300 Subject: [PATCH] =?UTF-8?q?corre=C3=A7=C3=A3o=20para=20criar=20usuario=20c?= =?UTF-8?q?om=20chave=20estrangeira?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/models/cargo.rb | 2 +- backend/app/models/user.rb | 2 +- .../20240518234753_create_user_join_table_cargos.rb | 5 ----- ...ate_cargos.rb => 20240520053452_create_cargos.rb} | 2 +- ...reate_users.rb => 20240520053455_create_users.rb} | 3 ++- backend/db/schema.rb | 12 ++++++------ backend/test/fixtures/cargos.yml | 4 ++-- backend/test/fixtures/users.yml | 6 ++++-- 8 files changed, 17 insertions(+), 19 deletions(-) delete mode 100644 backend/db/migrate/20240518234753_create_user_join_table_cargos.rb rename backend/db/migrate/{20240518231634_create_cargos.rb => 20240520053452_create_cargos.rb} (86%) rename backend/db/migrate/{20240516230139_create_users.rb => 20240520053455_create_users.rb} (77%) diff --git a/backend/app/models/cargo.rb b/backend/app/models/cargo.rb index 9432254..a78b63e 100644 --- a/backend/app/models/cargo.rb +++ b/backend/app/models/cargo.rb @@ -1,3 +1,3 @@ class Cargo < ApplicationRecord - has_many :user + has_many :users end diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index c87ee1e..fa04170 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -1,3 +1,3 @@ class User < ApplicationRecord - belongs_to :cargo + belongs_to :cargo end diff --git a/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb b/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb deleted file mode 100644 index 7e3083e..0000000 --- a/backend/db/migrate/20240518234753_create_user_join_table_cargos.rb +++ /dev/null @@ -1,5 +0,0 @@ -class CreateUserJoinTableCargos < ActiveRecord::Migration[7.1] - def change - add_reference :users, :cargos, foreign_key: true - end -end diff --git a/backend/db/migrate/20240518231634_create_cargos.rb b/backend/db/migrate/20240520053452_create_cargos.rb similarity index 86% rename from backend/db/migrate/20240518231634_create_cargos.rb rename to backend/db/migrate/20240520053452_create_cargos.rb index e295c9b..35b0f84 100644 --- a/backend/db/migrate/20240518231634_create_cargos.rb +++ b/backend/db/migrate/20240520053452_create_cargos.rb @@ -1,7 +1,7 @@ class CreateCargos < ActiveRecord::Migration[7.1] def change create_table :cargos do |t| - t.string :type + t.string :nome t.timestamps end diff --git a/backend/db/migrate/20240516230139_create_users.rb b/backend/db/migrate/20240520053455_create_users.rb similarity index 77% rename from backend/db/migrate/20240516230139_create_users.rb rename to backend/db/migrate/20240520053455_create_users.rb index c3de47b..29c5716 100644 --- a/backend/db/migrate/20240516230139_create_users.rb +++ b/backend/db/migrate/20240520053455_create_users.rb @@ -1,9 +1,10 @@ class CreateUsers < ActiveRecord::Migration[7.1] def change create_table :users do |t| - t.string :matricula t.string :nome + t.string :matricula t.string :email + t.references :cargo, null: false, foreign_key: true t.timestamps end diff --git a/backend/db/schema.rb b/backend/db/schema.rb index 1ea5aa0..2a718ed 100644 --- a/backend/db/schema.rb +++ b/backend/db/schema.rb @@ -10,25 +10,25 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_05_18_234753) do +ActiveRecord::Schema[7.1].define(version: 2024_05_20_053455) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "cargos", force: :cascade do |t| - t.string "type" + t.string "nome" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| - t.string "matricula" t.string "nome" + t.string "matricula" t.string "email" + t.bigint "cargo_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.bigint "cargos_id" - t.index ["cargos_id"], name: "index_users_on_cargos_id" + t.index ["cargo_id"], name: "index_users_on_cargo_id" end - add_foreign_key "users", "cargos", column: "cargos_id" + add_foreign_key "users", "cargos" end diff --git a/backend/test/fixtures/cargos.yml b/backend/test/fixtures/cargos.yml index af4cbf6..8982c94 100644 --- a/backend/test/fixtures/cargos.yml +++ b/backend/test/fixtures/cargos.yml @@ -1,7 +1,7 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - cargos: MyString + nome: MyString two: - cargos: MyString + nome: MyString diff --git a/backend/test/fixtures/users.yml b/backend/test/fixtures/users.yml index 8f47211..b9db350 100644 --- a/backend/test/fixtures/users.yml +++ b/backend/test/fixtures/users.yml @@ -1,11 +1,13 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - matricula: MyString nome: MyString + matricula: MyString email: MyString + cargo: one two: - matricula: MyString nome: MyString + matricula: MyString email: MyString + cargo: two