Skip to content

Commit 944d1e6

Browse files
committed
fixes inserts of records with no pkey
1 parent d366d6b commit 944d1e6

File tree

9 files changed

+38
-14
lines changed

9 files changed

+38
-14
lines changed

spec/migrations/mysql_migrations.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ CREATE UNIQUE INDEX projects_88fsssfsf ON projects (id);
7979

8080
CREATE TABLE user_projects(
8181
user_id INTEGER references users(id),
82-
project_id INTEGER references projects(id),
83-
created_at DATETIME,
84-
updated_at DATETIME
82+
project_id INTEGER references projects(id)
8583
);
8684

8785
CREATE TABLE things(

spec/migrations/pg_migrations.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ CREATE TABLE projects(
8686

8787
CREATE TABLE user_projects(
8888
user_id INTEGER,
89-
project_id INTEGER references projects(id),
90-
created_at timestamp without time zone,
91-
updated_at timestamp without time zone
89+
project_id INTEGER references projects(id)
9290
);
9391

9492
CREATE TABLE users_json(

spec/migrations/sqlite3_migrations.sql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ CREATE UNIQUE INDEX projects_88fsssfsf ON projects (id);
7373

7474
CREATE TABLE user_projects(
7575
user_id INTEGER references users(id),
76-
project_id INTEGER references projects(id),
77-
created_at DATETIME,
78-
updated_at DATETIME
76+
project_id INTEGER references projects(id)
7977
);
8078

8179
CREATE TABLE things(

spec/repo_spec.cr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ describe Crecto do
9191
changeset = Repo.insert(u)
9292
changeset.instance.id.should_not eq(nil)
9393
end
94+
95+
it "should insert records with no primary key or date fields" do
96+
project = Project.new
97+
project = Repo.insert(project).instance
98+
99+
up = UserProject.new
100+
up.user_id = 123
101+
up.project_id = project.id
102+
103+
changeset = Repo.insert(up)
104+
changeset.errors.empty?.should be_true
105+
end
94106
end
95107

96108
describe "#all" do
@@ -1056,6 +1068,8 @@ describe Crecto do
10561068
end
10571069

10581070
it "should delete THROUGH destroy dependents" do
1071+
Repo.delete_all(UserProject)
1072+
Repo.delete_all(Project)
10591073
Repo.delete_all(Post)
10601074
other_p = Project.new; other_p = Repo.insert(other_p).instance
10611075
other_up = UserProject.new; other_up.user_id = 999999; other_up.project_id = other_p.id; other_up = Repo.insert(other_up).instance

spec/spec_helper.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Project < Crecto::Model
5858
end
5959

6060
class UserProject < Crecto::Model
61+
set_created_at_field nil
62+
set_updated_at_field nil
63+
6164
schema "user_projects", primary_key: false do
6265
belongs_to :user, User
6366
belongs_to :project, Project

spec/transactions_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ describe Crecto do
3131
Repo.delete_all(Post)
3232
Repo.delete_all(User)
3333

34+
puts "\n\nusers: #{Repo.all(User)}\n\n"
35+
3436
user = quick_create_user("this should delete")
3537

38+
puts "\n\nusers: #{Repo.all(User)}\n\n"
39+
3640
multi = Multi.new
3741
multi.delete(user)
3842
Repo.transaction(multi)

src/crecto/adapters/mysql_adapter.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ module Crecto
4848

4949
query = exec_execute(conn, q.join(" "), fields_values[:values])
5050
return query if conn.is_a?(DB::TopLevelTransaction)
51+
5152
if changeset.instance.class.use_primary_key?
5253
last_insert_id = changeset.instance.pkey_value.nil? ? "LAST_INSERT_ID()" : "'#{changeset.instance.pkey_value.not_nil!}'"
5354
execute(conn, "SELECT * FROM #{changeset.instance.class.table_name} WHERE #{changeset.instance.class.primary_key_field} = #{last_insert_id}")
55+
else
56+
query
5457
end
5558
end
5659

src/crecto/adapters/sqlite3_adapter.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ module Crecto
5050
if changeset.instance.class.use_primary_key?
5151
last_insert_id = changeset.instance.pkey_value.nil? ? res.last_insert_id : changeset.instance.pkey_value.not_nil!
5252
execute(conn, "SELECT * FROM #{changeset.instance.class.table_name} WHERE #{changeset.instance.class.primary_key_field} = '#{last_insert_id}'")
53+
else
54+
res
5355
end
5456
end
5557

src/crecto/repo.cr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module Crecto
88

99
def to_h
1010
{
11-
:message => @message.to_s,
12-
:queryable => @queryable.to_s,
13-
:failed_operation => @failed_operation
11+
:message => @message.to_s,
12+
:queryable => @queryable.to_s,
13+
:failed_operation => @failed_operation,
1414
}
1515
end
1616
end
@@ -228,8 +228,12 @@ module Crecto
228228
changeset.add_error("insert_error", "Insert Failed")
229229
elsif config.adapter == Crecto::Adapters::Postgres || (config.adapter == Crecto::Adapters::Mysql && tx.nil?) ||
230230
(config.adapter == Crecto::Adapters::SQLite3 && tx.nil?)
231-
new_instance = changeset.instance.class.from_rs(query.as(DB::ResultSet)).first?
232-
changeset = new_instance.class.changeset(new_instance) if new_instance
231+
if query.is_a?(DB::ResultSet)
232+
new_instance = changeset.instance.class.from_rs(query.as(DB::ResultSet)).first?
233+
changeset = new_instance.class.changeset(new_instance) if new_instance
234+
else
235+
changeset = queryable_instance.class.changeset(queryable_instance)
236+
end
233237
end
234238
rescue e
235239
raise e unless changeset.check_unique_constraint_from_exception!(e, queryable_instance)

0 commit comments

Comments
 (0)