File tree Expand file tree Collapse file tree 9 files changed +38
-14
lines changed Expand file tree Collapse file tree 9 files changed +38
-14
lines changed Original file line number Diff line number Diff line change @@ -79,9 +79,7 @@ CREATE UNIQUE INDEX projects_88fsssfsf ON projects (id);
79
79
80
80
CREATE TABLE user_projects (
81
81
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)
85
83
);
86
84
87
85
CREATE TABLE things (
Original file line number Diff line number Diff line change @@ -86,9 +86,7 @@ CREATE TABLE projects(
86
86
87
87
CREATE TABLE user_projects (
88
88
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)
92
90
);
93
91
94
92
CREATE TABLE users_json (
Original file line number Diff line number Diff line change @@ -73,9 +73,7 @@ CREATE UNIQUE INDEX projects_88fsssfsf ON projects (id);
73
73
74
74
CREATE TABLE user_projects (
75
75
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)
79
77
);
80
78
81
79
CREATE TABLE things (
Original file line number Diff line number Diff line change @@ -91,6 +91,18 @@ describe Crecto do
91
91
changeset = Repo .insert(u)
92
92
changeset.instance.id.should_not eq(nil )
93
93
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
94
106
end
95
107
96
108
describe " #all" do
@@ -1056,6 +1068,8 @@ describe Crecto do
1056
1068
end
1057
1069
1058
1070
it " should delete THROUGH destroy dependents" do
1071
+ Repo .delete_all(UserProject )
1072
+ Repo .delete_all(Project )
1059
1073
Repo .delete_all(Post )
1060
1074
other_p = Project .new; other_p = Repo .insert(other_p).instance
1061
1075
other_up = UserProject .new; other_up.user_id = 999999 ; other_up.project_id = other_p.id; other_up = Repo .insert(other_up).instance
Original file line number Diff line number Diff line change @@ -58,6 +58,9 @@ class Project < Crecto::Model
58
58
end
59
59
60
60
class UserProject < Crecto::Model
61
+ set_created_at_field nil
62
+ set_updated_at_field nil
63
+
61
64
schema " user_projects" , primary_key: false do
62
65
belongs_to :user , User
63
66
belongs_to :project , Project
Original file line number Diff line number Diff line change @@ -31,8 +31,12 @@ describe Crecto do
31
31
Repo .delete_all(Post )
32
32
Repo .delete_all(User )
33
33
34
+ puts " \n\n users: #{ Repo .all(User )} \n\n "
35
+
34
36
user = quick_create_user(" this should delete" )
35
37
38
+ puts " \n\n users: #{ Repo .all(User )} \n\n "
39
+
36
40
multi = Multi .new
37
41
multi.delete(user)
38
42
Repo .transaction(multi)
Original file line number Diff line number Diff line change @@ -48,9 +48,12 @@ module Crecto
48
48
49
49
query = exec_execute(conn, q.join(" " ), fields_values[:values ])
50
50
return query if conn.is_a?(DB ::TopLevelTransaction )
51
+
51
52
if changeset.instance.class.use_primary_key?
52
53
last_insert_id = changeset.instance.pkey_value.nil? ? " LAST_INSERT_ID()" : " '#{ changeset.instance.pkey_value.not_nil! } '"
53
54
execute(conn, " SELECT * FROM #{ changeset.instance.class.table_name } WHERE #{ changeset.instance.class.primary_key_field } = #{ last_insert_id } " )
55
+ else
56
+ query
54
57
end
55
58
end
56
59
Original file line number Diff line number Diff line change @@ -50,6 +50,8 @@ module Crecto
50
50
if changeset.instance.class.use_primary_key?
51
51
last_insert_id = changeset.instance.pkey_value.nil? ? res.last_insert_id : changeset.instance.pkey_value.not_nil!
52
52
execute(conn, " SELECT * FROM #{ changeset.instance.class.table_name } WHERE #{ changeset.instance.class.primary_key_field } = '#{ last_insert_id } '" )
53
+ else
54
+ res
53
55
end
54
56
end
55
57
Original file line number Diff line number Diff line change @@ -8,9 +8,9 @@ module Crecto
8
8
9
9
def to_h
10
10
{
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 ,
14
14
}
15
15
end
16
16
end
@@ -228,8 +228,12 @@ module Crecto
228
228
changeset.add_error(" insert_error" , " Insert Failed" )
229
229
elsif config.adapter == Crecto ::Adapters ::Postgres || (config.adapter == Crecto ::Adapters ::Mysql && tx.nil?) ||
230
230
(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
233
237
end
234
238
rescue e
235
239
raise e unless changeset.check_unique_constraint_from_exception!(e, queryable_instance)
You can’t perform that action at this time.
0 commit comments