Can you define a maybe property that is not nullable in Yesod Persist? - yesod

I am still quite new to Yesod so I might be missing something essential.
My understanding is that adding the Maybe attribute to a Persist Entity field seems to both make the attribute non nullable in the database as well as wrapping it in a Maybe in the Data definition. I tried creating a test Entity with a timestamp that has a default=CURRENT_TIME. If I now send a JSON representation of this entity without the timestamp, I get an error about the missing timestamp when I try to use requireJsonBody. I guess I could add Maybe to the timestamp to get the JSON parsing to succeed. But then I would not have the non null constraint for the column in the database anymore?

Related

Migrating Django TextChoices to IntegerChoices

In Django (3.2.8), I have a set of TextChoices, which I would like to change to IntegerChoices. I will keep the keys the same, just the values in the database stored will just differ. I do this for consistency and I'm fixing some legacy code.
Naturally, I changed the TextChoices to IntegerChoices, and the corresponding field to an IntegerField. As expected however, upon migrating, I get a django.db.utils.DataError: invalid input syntax for type integer: "option1". I feel like there should be a way that I can define a mapping for this migration to happen, but wasn't able to find any documentation on this.

How to get the date when the particular attribute was first changed in rails

I have a boolean atrribute whose default is false. How can i get the date when the attribute was changed to true?
The is_changed gives you if the value was changed. I want the date when the attribute was first changed.
How do i get it in rails?
add attribute 'first_change' in that table which will save time stamp of every first change of the boolean attribute changed to true or false, then in model write the callback like this
before_update :check_changes
def check_changes
if self.<boolean_attribute>.is_changed? and first_change.nil?
self.update(first_change: Time.now)
end
end
After this you can check when the boolean attribute was changed.
You can wrap your boolean attribute in distinct model and from there you can easily trace when specific field was initially setted up or updated. In your current model you can't trace changing state of specific attributes, only the whole object, but it's not what you need I guess.
Well, looks like you basically want the feature of git in ActiveRecord records.
There are two ways
Use a separate col like changed_date. Update it whenever the field is changed for the first time.
I recommend this if your requirement is that simple. Do not use heavier gems.
Use libraries like VestalVersion or PaperTrail
these are helpful to track every activity in your records.
you can keep track of every changes, what its changed to and when
also you can revert your record to any point of time it was changed
I think there are two ways like above answer suggested to make a separate field and add date there when field changed first time or
you can use Public Activity gem that will log all the model activity with params.
It creates a activities table based on this table you can get the date of the fields when it was first changed but it is a lengthy process

merge in doctrine 2 not working giving error

I am trying to update a row in a table. I am using doctrine 2 ORM. I am trying to update a row using merge(), which is said can be used to update a row. But it gives a error saying
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
I am new to doctrine 2. please suggest what can i do?
When to merge
First off: You only need $em->merge() when you have an entity that isn't managed by the EntityManager, but you want it to be. A common use-case is when you have a serialized entity, and want the EntityManager to start managing it.
So merging entities is not directly related to updating entities in the database.
If you simply find (using $repo->find*()) an entity and make changes, calling $em-flush() is sufficient. The entity is already managed by the EntityManager and there's no need to merge it.
How to merge
A common mistake when using $em->merge() is that the passed entity itself becomes managed. This isn't true, $em->merge() returns a new object that represents the managed entity.
$managedEntity = $em->merge($detachedEntity);
After this line of code, $detachedEntity is still detached (meaning it still isn't managed by the EntityManager). It's $managedEntity which you can start using to make changes.
Your code
Given the code you've put in the comments, you probably want to do something like this:
$user = $entityManager->getRepository('User')->find($_REQUEST['id']);
$user->setName($_REQUEST['name']);
$user->setPassword($_REQUEST['pass']);
$entityManager->flush();
PS: It looks like you're saving the the plain-text password in the database. That's never a good idea.

How to handle EntityNotFoundException in Spring Data JPA (or just standard JPA 2)

I am in the process of cleaning a database. These process involves changing the format of certain fields and getting rid of some data integrity issues.
I developed a program with Spring Data 1.1 to process the records in batches. The problem arises with 2 entities in a #OneToOne relationship. The record for Entity B does not exist although Entity A has a reference to it. My job is to clear the reference to Entity B if that is the case.
The question is: should I pre-process the data to clean this or can I adjust Spring Data or JPA settings to put null in the field if the Entity is not found?
It is "normal" - with this data - to have a FK in Entity A that does not exist in Entity B, so I want to handle this in my code and not have to pre-process the data with an additional step or other tool. The data will be arriving in batches so any pre-processing makes things more complicated for the user.
In summary, I want Spring Data to set the field to null and continue the process instead of getting an org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find....
Perhaps you are looking for the #NotFound annotation?
Here is a post that talks about it.
I have had the same problem because my orm mapping has a wrong #One-to-one unidirectional relationship.

Subsonic 3.0 with timestamp field in SQL 2005

I have a timestamp field in a table and when i create a new instance of my object, set some fields, do not set the timestamp field and use the .Save(), i get an error saying that i cannot set an exlicite value to a timestamp field during an INSERT.
How can i have SubSonic not save anything in that perticular field?
Thank you
I'm not sure I perfectly understand the question, but the default SubSonic templates are set up to treat certain date/time fields as special cases, those being CreatedOn and ModifiedOn. You could look through your T4 template for references to those fields and modify their behavior.
Does this even apply to your situation, or are your timestamp fields named something else completely?
this is what you are looking for
http://mvcframeworkblog.blogspot.com/2009/10/subsonic-how-to-add-new-datatype.html