Example I have entity orders.
As a request I am expecting only order num and quantity from orders class.
I see in log as:
{
Order ID : null,
Order num : 'O0001',
Qty : '20',
Optional : null
LastUpdated : null
}
Since only order num and Qty was entered by user, I want to hide other non passed values in log.
Please suggest how can that be made.
You can add the below Annotation to your Entity class.This would hide the null values.
import com.fasterxml.jackson.annotation.JsonInclude;
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Order{
Related
null value in column "user_id" violates not-null constraint
DETAIL: Failing row contains (1, 10000, 11200, null).
this is my models.py
id =models.CharField(max_length=6, primary_key = True, editable=False, unique=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
I was trying to return a Json Response
insert null = True in your id model, this will correct the integrity error.
You said you were trying to return a JSON response; was this saved in some way or another?
This error indicates that nothing is passed to the user field on save. Hence, you need to give the user field a user instance before saving. Can we see what you are trying to do in order to know how to fix the problem?
i am trying to get barcode variable while qty variable is 0 or !=.Like Select barcode Where qty=0 or Select barcode WHERE qty=0. I spend whole my day but i didnt figure out how to make this. Please help me
#GetMapping(path = "/barcode/{barcode}")
public List<User> findd(#PathVariable("barcode") String barcode,
#RequestParam Integer qty) {
return repository.findByQty(qty);
}
s
public interface UserRepository extends JpaRepository<User,Integer> {
User findByBarcode(String barcode);
List <User> findByQty(int qty);
}
You are just fetching the results by quantity here which is absolutely wrong.
There are multiple products having quantity as 0.
So you must pass quantity and barcode both to fetch results.
For example:
Select barcode from table_name where barcode=666 and qty=0.
At the controller method level, you can easily pass path variable the same way as you are passing qty.
I am new to Grails. I installed Grails 2.4.4 version and Spring Tool Suite.
I created Grails project and domain ,controller classes. But i am getting all the fields as mandatory by default and "id" is not generated. Need some help.
Domain Class:
package org.example.pomorodo
class Task {
String summary
String details
Date dateCreated
Date deadLine
Long timeSpent=0L
static constraints = {
}
}
Controller Class:
package org.example.pomorodo
class TaskController {
static scaffold =Task
}
Do you mean you can't access the "id" property after saving your domain object with GORM? Or just that the generated code doesn't have an "id" property explicitly defined? If it's the latter, don't worry. The "id" property is automatically injected by metaprogramming black-magic by the framework. If it's the former, something else is wrong, as you should definitely have a valid "id" after saving a domain object.
On the mandatory fields: Use the constraints block to toggle fields to nullable = true. Example:
package org.example.pomorodo
class Task {
String summary
String details
Date dateCreated
Date deadLine
Long timeSpent=0L
static constraints = {
summary(nullable:true)
details(nullable:true)
}
}
You can also modify default constraints globally. See the constraints documentation for more details.
Recently I got a query regarding mapping a database table which do not have any id and version. For example the table have two varchar fields username and password nothing more than that.
Although it was something strange for me that table doesn’t have the id field. The good thing is that the username is a primary key in the table and this is not auto incremented user want to create it by his own method.
The good thing about grails is, in most of the cases you get your answer in the docs http://grails.org/doc/latest/ . So in this case we just need to change the id field in grails domain like this
class Test {
String username
String password
static mapping = {
id name: 'username'
version false
id generator: 'assigned'
}
static constraints = {
username(nullable: true)
}
}
I have created sqlalchemy class and wtform Form from class.
from wtforms.ext.sqlalchemy.orm import model_form
class ClientGroup(Base, BaseModel):
__tablename__ = 'client_groups'
id = Column(Integer, primary_key=True)
name = Column(String(255))
sale = Column(Integer)
def __unicode__(self):
return self.name
ClientGroupForm = model_form(ClientGroup, Form, field_args={
'name': {'validators' : [validators.Required(), validators.Length(min=5)]},
})
in app I have following code:
form = ClientGroupForm(request.form)
if form.validate():
.. save data
When i send a new data to app its returns me validation error. ID must be an integer field.
But a new data hasn't any id.
Please, give me advice How can I use wtforms with sqlalchemy?
Make it optional explicitly, by adding the following to the field_args:
'id': {'validators' : [validators.Optional()],
However, I think you should exclude the id field from the form completely:
ClientGroupForm = model_form(ClientGroup, Form, field_args={
'name': {'validators' : [validators.Required(), validators.Length(min=5)]},
exclude=['id'],
})
I have a form changeed the properties of my object CUSTOMER. Each customer has related ORDERS. The ORDER's table has a column customer_id which is used for the mapping. All works so far, I can read customers without any problem.
When I now e.g. change the name of the CUSTOMER in the form (which does NOT show the orders), after saving the name is updated, but all relations in the ORDERS table are set to NULL (the customer_id for the items is set to NULL.
How can I keep the relationship working?
THX
UPDATE: Mapping Info
The Orders are mapped on the Customer side
#OneToMany
#JoinColumn(name = "customer_id")
#OrderBy("orderDate")
private Collection<Order> orders = new LinkedList<Order>();
UPDATE
Seems like adding a
#SessionAttributes("customer")
to my model, changing the method to
public String saveTrip(#ModelAttribute("customer") Customer customer, BindingResult result, SessionStatus status) {
if (!result.hasErrors()) {
this.tripManager.saveTrip(trip);
}
else {
logger.debug("Form data included errors, did not save data");
BindingUtils.logBindingErrors(result, logger);
}
status.setComplete();
return "redirect:/customers/";
}
Could solve the issu. But is this a good way of solving it???
One way would be not to submit the CUSTOMER Object from the form.
Instead submit the customer, submit only the customers ID and the new Name. In the controller you have to load the Customer by the submitted ID and then update the Name. And persist the Customer again.
HI,
Make cascade="none" attribute of many-to-one relationship from order side.
Thanks.